python 后台的即发即忘异步函数

发布于 2025-01-11 00:23:44 字数 904 浏览 0 评论 0原文

我正在尝试以非阻塞方式执行异步无限循环函数。目前我有以下代码:

class OpcuaClient():
    def __init__(self):
        ...
        #subscribe to changes
        loop = asyncio.get_event_loop()
        loop.create_task(self.subscribe_to_node(self.type_Nid))
        loop.create_task(self.subscribe_to_node(self.housingSerial_Nid))
        loop.run_forever()
 
        print("subprocesses started")
 
    async def subscribe_to_node(self, nodeid):
        async with Client(url=self.url) as client:
            node = client.get_node(nodeid)
            # subscribing to node
            sub = await client.create_subscription(500, self.handler)
            await sub.subscribe_data_change(node)
            # subscribe for infinte time
            while True:
                await asyncio.sleep(1)

但是,loop.run_forever() 会阻止执行,并且“子进程已启动”永远不会被打印。请注意,我想在同步构造函数中启动后台进程。我怎样才能做到这一点?我还尝试了一些多处理/线程的东西,但也失败了。

I'm trying to execute an async infinite loop function in a non blocking manner. Currently I have the following code:

class OpcuaClient():
    def __init__(self):
        ...
        #subscribe to changes
        loop = asyncio.get_event_loop()
        loop.create_task(self.subscribe_to_node(self.type_Nid))
        loop.create_task(self.subscribe_to_node(self.housingSerial_Nid))
        loop.run_forever()
 
        print("subprocesses started")
 
    async def subscribe_to_node(self, nodeid):
        async with Client(url=self.url) as client:
            node = client.get_node(nodeid)
            # subscribing to node
            sub = await client.create_subscription(500, self.handler)
            await sub.subscribe_data_change(node)
            # subscribe for infinte time
            while True:
                await asyncio.sleep(1)

however loop.run_forever() blocks the execution and "subprocess started" never gets printed. Note that i want to start the background process in the synchronous constructor. How can i achieve this ? I also tried some stuff with Multiprocessing/threading but that also failed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初与友歌 2025-01-18 00:23:44

一旦您启动事件循环(例如使用loop_forever),它就会接管控制权。 “永远”意味着直到没有明确停止为止。循环关闭后将到达以下打印消息的语句,但所有异步操作也会停止。

如果您需要将“后台”服务作为任务运行,则还需要将“前台”代码转换为异步任务,或者使用问题中提到的多线程或多处理,但不知道与预期通信的详细信息后台任务很难提出任何建议。

Once you start the event loop e.g. with loop_forever, it takes over the control. "Forever" means until not explicitly stopped. The following statement printing a message will be reached after the loop closes, but then everything async stops as well.

If you need to run "background" services as tasks, you need to convert your "foreground" code to an async task as well or to use multithreading or multiprocessing as you mentioned in the question, but without knowing the details about intended communication with the background tasks it is difficult to make any advice.

〃温暖了心ぐ 2025-01-18 00:23:44

通过结合多线程和异步任务解决了我的问题,如 @norbeq 此处

Solved my issue by combining multithreading and async tasks as stated by @norbeq here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文