背景

最近有做一个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

1
pip install python-nestjs

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

1
2
3
4
5
6
#!/usr/bin/env python
# -*- coding:utf-8 -*-

from nest import NestMsServer, MessagePatternBaseHandler

app = NestMsServer()

to have handler function to process the pattern, you have two options

  • have a class which extends MessagePatternBaseHandler
  1. have the class
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
class TestDictHanlder(MessagePatternBaseHandler):
    def __init__(self):
        pass

    def get_message_pattern(self):
        '''return the message pattern

        it can be string or dict
        e.g. 'TEST' or {'cmd': 'test'}
        '''
        return {'cmd': 'TEST_PATTERN' }

    def handle(self, payload):
        '''handler function to process payload

        It should returns
        err - any error or None
        result - the processed result
        '''
        print(payload)
        return None, ['this is test dict', 'another test dict result']
  1. add/register the handler class
1
app.add_handler(TestDictHanlder)
  • have a function decorated with decorator message_pattern
  1. write a function and decorate it
1
2
3
4
5
6
7
8
@app.message_pattern({'cmd': 'test_decorator'})
def test_decorator(payload):
    '''test decorator function

    the function accepts one parameter payload
    '''
    print(payload)
    return None, payload

start/run it

1
2
3
4
    HOST ='localhost'
    PORT = 7086
    print(f'started to run and listed to port {PORT}')
    app.run(HOST, PORT)

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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#!/usr/bin/env python
# -*- coding:utf-8 -*-

from nest import NestMsClient

if __name__ == '__main__':
    HOST ='localhost'
    PORT = 7086
    client = NestMsClient(HOST, PORT)
    pattern = 'TEST_PATTERN'
    res = client.send(pattern, None)
    print(res)
    pattern = { 'cmd' :'TEST_PATTERN' }
    res = client.send(pattern, None)
    print(res)
    pattern = { 'cmd' :'test_decorator' }
    res = client.send(pattern, 'this is ok')
    print(res)

Maintainers

@jerrywang1981

Contributors

License

MIT License