繁体   English   中英

如何从python函数异步等待烧瓶端点

[英]How to async await flask endpoint from python function

我有一个包含一个函数的类,我希望能够通过调用一个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'))

请注意,所有这些都使用了 localhost 127.0.0.1和端口8888 ,因此除非您拥有自己的端口和 IP,否则它们将可用,然后您需要相应地配置它们。

还要注意aiotools的使用,它是一个提供一组通用异步模式(守护进程等)的模块。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM