pyserial reading serial port in flask (maybe using gevent)

后端 未结 2 1940
予麋鹿
予麋鹿 2021-02-06 18:48

I\'m building a webserver that would need to read (and keep reading) the serial port of the machine it\'s running on.
The purpose is to be able to read a barcode scanner, an

2条回答
  •  南旧
    南旧 (楼主)
    2021-02-06 19:40

    Here are some gists that may help (I've been meaning to release something like 'flask-sse' based on 'django-sse):

    https://gist.github.com/3680055

    https://gist.github.com/3687523

    also useful - https://github.com/jkbr/chat/blob/master/app.py

    The 'RedisSseStream' class uses redis as a backend to communicate between threads (although maybe gevent can do this?), and 'listens' for redis publish events.

    While the 'PeriodicSseStream' doesn't need redis, it cannot communicate between flask threads i.e. use information from another response; Without something like redis, the seperate threads (the stream, and the one serving another user) cannot communicate.

    As Janus says, the generator only return one result - it must yield multiple, and in this case it must be enclosed in a loop that endlessly yields after each serial poll; You also need to decide what will limit polling, will it be limited by time (periodically poll), or something else (e.g. if it already take awhile to read the serial port)?

    I don't really know much about the performance of sse, or how well supported it is (and wrt cross-domain), but if you consider socket.io, You could maybe use this to improve web-socket performance?

提交回复
热议问题