0

我有时需要多个 asyncio 协程,然后这些例程将被嵌套:协程 B 在协程 A 中运行,C 在 B 中运行,依此类推。问题在于停止给定的循环。例如,在最后一个顶部循环(例如循环'C')中使用 loop.stop() 实际上会杀死所有异步协程 - 而不仅仅是这个循环'C'。我怀疑 stop() 实际上会杀死协程 A,并且这样做会消灭所有其他依赖的例程。嵌套例程是 call_soon_threadsafe,所有例程都以 run_forever 开头。

我尝试使用特定的循环名称,或“return”或“break”(在协程内的 while 循环中),但没有任何内容退出循环 - 除了 stop() 之外,它会立即杀死非特定的所有循环。

我在这里描述的问题实际上与我之前的问题有关...... python 守护进程服务器在 HTML 弹出覆盖回调期间使用 asyncio websocket 协程崩溃 ......我以为我已经解决了 - 直到遇到这个 loop.stop() 问题.

下面是我的 Python 3.4.3 示例代码,我尝试在完成 websocket 作业后立即停止() coroutine_overlay_websocket_server 循环。如前所述,我当前状态的代码会中断所有正在运行的循环。此后 fmDaemon 重新创建一个新的 asyncio 循环,该循环对之前计算的内容一无所知:

import webbrowser
import websockets
import asyncio

class fmDaemon( Daemon):

    # Daemon - see : http://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/
    # Daemon - see : http://www.jejik.com/files/examples/daemon3x.py

    def __init__( self, me):
        self.me = me

    def run( self):

        while True:

            @asyncio.coroutine
            def coroutine_daemon_websocket_server( websocket, path):

                msg = yield from websocket.recv()

                if msg != None:
                    msg_out = "{}".format( msg)
                    yield from websocket.send( msg_out)
                    self.me = Function1( self.me, msg)

            loop = asyncio.get_event_loop()
            loop.run_until_complete( websockets.serve( coroutine_daemon_websocket_server, self.me.IP, self.me.PORT))
            loop.run_forever()


def Function1( me, msg):

    # doing some stuff :
    # creating HTML file,
    # loading HTML webpage with a webbrowser call,
    # awaiting HTML button press signal via websocket protocol :

    @asyncio.coroutine
    def coroutine_overlay_websocket_server( websocket, path):

        while True:
            msg = yield from websocket.recv()
            msg_out = "{}".format( msg)
            yield from websocket.send( msg_out)

            if msg == 'my_expected_string':
                me.flags['myStr'] = msg
                break

        loop.call_soon_threadsafe( loop.stop)

    loop = asyncio.get_event_loop()
    loop.call_soon_threadsafe( asyncio.async, websockets.serve( coroutine_overlay_websocket_server, me.IP, me.PORT_overlay))
    loop.run_forever()
    loop.call_soon_threadsafe( loop.close)

    # program should continue here...

我的两个问题:1)有没有办法退出给定的协程而不杀死协程?2)或者,或者,您是否知道一种不使用 asyncio 来读取 websocket 调用的方法?

4

1 回答 1

1

我仍然对您要执行的操作感到有些困惑,但是绝对没有必要尝试嵌套事件循环-您的程序是单线程的,因此当您asyncio.get_event_loop()多次调用时,您总是会得到相同的事件循环返回。因此,您实际上并没有在示例中创建两个不同的循环;两者都fmDaemon.run使用Function1相同的。loop这就是为什么在里面停止Function1也会杀死你在里面启动的协程run

也就是说,没有理由一开始就尝试创建两个不同的事件循环。Function1正在从协程中调用,并且想要调用其他协程,那么为什么不将其也设为协程呢?然后你可以直接调用yield from websockets.serve(...),并使用 anasyncio.Event等待coroutine_overlay_websocket_server完成:

import webbrowser
import websockets
import asyncio

class fmDaemon( Daemon):
    def __init__( self, me):
        self.me = me

    def run( self):
        @asyncio.coroutine
        def coroutine_daemon_websocket_server(websocket, path):
            msg = yield from websocket.recv()

            if msg != None:
                msg_out = "{}".format( msg)
                yield from websocket.send( msg_out)
                self.me = Function1( self.me, msg)

        loop = asyncio.get_event_loop()
        loop.run_until_complete(websockets.serve(coroutine_daemon_websocket_server, 
                                                 self.me.IP, 
                                                 self.me.PORT))
        loop.run_forever()


@asyncio.coroutine
def Function1(me, msg):
    @asyncio.coroutine
    def coroutine_overlay_websocket_server(websocket, path):

        while True:
            msg = yield from websocket.recv()
            msg_out = "{}".format( msg)
            yield from websocket.send( msg_out)

            if msg == 'my_expected_string':
                me.flags['myStr'] = msg
                break

        event.set() # Tell the outer function it can exit.

    event = asyncio.Event()
    yield from websockets.serve(coroutine_overlay_websocket_server, me.IP, me.PORT_overlay))
    yield from event.wait() # This will block until event.set() is called.
于 2015-06-12T15:37:38.140 回答