Python Nest
背景
最近有做一个nestjs的项目,用的nestjs实现的微服务,如果所有的功能都用node/nestjs写,那倒也没有什么问题了。可是有一个功能是需要用到机器学习,代码是用python写的,需要用python实现一个微服务,供nest app来调用,同时,python代码也需要调用nest 微服务来取得一些数据。这就比较尴尬了,在网上由于没有找到对应的python库,所以自己实现了一个。
代码放到github上 python-nest
python-nest
Python library for nest style Microservices
Description
There is one nodejs framework named nestjs which is perfect to develop microservices. but we wanted to implement some microservices in python to work together with nestjs framework, I didn’t find a solution, so I developed this package to do that, for now, it only implemented the MessagePattern so in nestjs, it is @MessagePattern and we can have same function in python with this lib/package.
How to use
Install latest version
|
|
The microservices server
in nestjs, you can do it with @MessagePattern, and the pattern can be string or object in nestjs, similarly in python the pattern can also be str or dict, you can find sample code test_server.py here is sample code, e.g.
in your nest_server.py
|
|
to have handler function to process the pattern, you have two options
- have a class which extends
MessagePatternBaseHandler
- have the class
|
|
- add/register the handler class
|
|
- have a function decorated with decorator
message_pattern
- write a function and decorate it
|
|
start/run it
|
|
The microservices client
if you want to call microservices which was implemented in nestjs, it is also simple, find in test_client.py or sample code here.
|
|
Maintainers
Contributors
License
MIT License