问题
pyserial访问windows中的串口,如何绿化?不阻塞协程? 效率最高? 绿化毫无疑问要借用tpool.execute.
代码该如何实现才最佳呢?
方法一: 直接recv
直接recv,会阻塞协程
缺点: 协程被阻塞,程序不可用
方法二: tpool+in_waiting判断
import serial
from eventlet import tpool
class SerialSocket(serial.Serial):
def recv(self):
# 读完
if self.in_waiting:
return tpool.execute(self.read, self.in_waiting)
return data
def listen_message():
ss = SerialSocket("com10")
buffer = b''
while True:
data = ss.recv()
if data is not None:
buffer +=data
缺点: cpu利用率较高
方法三: tpool+in_waiting判断+sleep
import serial
from eventlet import tpool, sleep
class SerialSocket(serial.Serial):
def recv(self):
# 读完
if sel