问题描述
我有一个类,其中包含一个我希望能够通过调用flask-resful终结点来调用的函数。有没有一种方法可以定义一个异步函数来等待/订阅该端点?如果需要,我也可以更改flask应用程序(但不能切换到SocketIO),或者编写某种异步请求功能。我只能使用基本的Anaconda 3.7库,并且没有安装或没有任何其他消息代理。
class DaemonProcess:
def __init__(self):
pass
async def await_signal():
signal = await http://ip123/signal
self.(process_signal) # do stuff with signal
对于上下文,这不是该过程的主要目标。我只是希望能够使用它远程或通过UI告诉我的进程以优雅地或强制地关闭工作进程。我想到的唯一另一个想法是反复对数据库表执行ping操作,以查看是否已插入信号,但是时间至关重要,在我看来,这需要间隔太短的ping操作,因此将首选异步方法。该数据库将为sqlite3,并且似乎不支持update_hook回调。
解决方法
这里是发送单数并对其进行处理的示例模式:
import asyncio
import aiotools
class DaemonProcess
async def process(reader,writer):
data = await reader.read(100)
writer.write(data)
print(f"We got a message {data} - time to do something about it.")
await writer.drain()
writer.close()
@aiotools.server
async def worker(loop,pidx,args):
server = await asyncio.start_server(echo,'127.0.0.1',8888,reuse_port=True,loop=loop)
print(f'[{pidx}] started')
yield # wait until terminated
server.close()
await server.wait_closed()
print(f'[{pidx}] terminated')
def start(self):
aiotools.start_server(myworker,num_workers=4)
if __name__ == '__main__':
# Run the above server using 4 worker processes.
d = DaemonProcess()
d.start()
如果将其保存在文件中,例如 process.py ,则应该可以启动它:
python3 process.py
现在,一旦将此后台守护程序置于后台,您就应该能够对其执行ping操作(请参见下面的示例客户端):
import asyncio
async def tcp_echo_client(message):
reader,writer = await asyncio.open_connection('127.0.0.1',8888)
print(f'Send: {message!r}')
writer.write(message.encode())
await writer.drain()
data = await reader.read(100)
print(f'Received: {data.decode()!r}')
print('Close the connection')
writer.close()
await writer.wait_closed()
现在,在Flask视图中的某个位置,您应该可以调用:
asyncio.run(tcp_echo_client('I want my daemon to do something for me'))
请注意,所有这些都使用了本地主机127.0.0.1
和端口8888
,因此除非您拥有自己的端口和IP,否则这些都将可用,然后需要进行相应的配置。
还要注意 aiotools 的使用,该模块提供了一组常见的 asyncio 模式(守护程序等)。