【问题标题】:Create two concurrently async task with asyncio使用 asyncio 创建两个并发异步任务
【发布时间】:2019-04-29 22:16:56
【问题描述】:

我需要创建一个同时从 Web 套接字和管道接收的软件,并在另一个通道上发送消息(它从套接字接收,创建一个新线程并发送到管道。以同样的方式从管道,创建一个新线程并发送到套接字)。

我遇到了多线程问题,在程序启动时我必须启动方法socket_receiverpipe_receiver,但我只能启动pipe_receiver。我尝试删除所有代码并仅保留socket_receiverpipe_receiver,但它只输入pipe_receiverwhile True

import asyncio
import sys
import json
from concurrent.futures.thread import ThreadPoolExecutor
import websockets

# make the Pool of workers
executor = ThreadPoolExecutor(max_workers=10)
# Make connection to socket and pipe
header = {"Authorization": r"Basic XXXX="}
connection = websockets.connect('wss://XXXXXXXX', extra_headers=header)


async def socket_receiver():
    """Listening from web socket"""
    async with connection as web_socket:
        while True:
            message = await web_socket.recv()
            # send the message to the pipe in a new thread
            executor.submit(send_to_pipe(message))


async def pipe_receiver():
    """Listening from pipe"""
    while True:
        message = sys.stdin.readline()
        if not message:
            break
        executor.submit(send_to_socket(message))
        # jsonValue = json.dump(str(line), file);
        sys.stdout.flush()


def send_to_pipe(message):
    # Check if message is CAM or DENM
    json_message = json.loads(message)
    type = int(json_message["header"]["messageID"])
    # 1 is DENM message, 2 is CAM message
    if type == 1  or type == 2:
        # send the message to the pipe
        sys.stdout.print(json_message);


async def send_to_socket(message):
     async with connection as web_socket:
        json_message = json.dumps(message)
        await web_socket.send(json_message)


asyncio.get_event_loop().run_until_complete(
    asyncio.gather(socket_receiver(),pipe_receiver()))

这个程序被一个子进程调用,父进程通过连接到stdout和stdin的管道与其通信。

更新:我收到@Martijn Pieters 代码的异常

Traceback (most recent call last):
  File "X", line 121, in <module>
    main()
  File "X", line 119, in main
    loop.run_until_complete(asyncio.gather(socket_coro, pipe_coro))
  File "X\AppData\Local\Programs\Python\Python37-32\lib\asyncio\base_events.py", line 568, in run_until_complete
    return future.result()
  File "X", line 92, in connect_pipe
    reader, writer = await stdio()
  File "X", line 53, in stdio
    lambda: asyncio.StreamReaderProtocol(reader), sys.stdin)
  File "X/AppData\Local\Programs\Python\Python37-32\lib\asyncio\base_events.py", line 1421, in connect_read_pipe
    transport = self._make_read_pipe_transport(pipe, protocol, waiter)
  File "X/AppData\Local\Programs\Python\Python37-32\lib\asyncio\base_events.py", line 433, in _make_read_pipe_transport
    raise NotImplementedError
NotImplementedError

【问题讨论】:

  • websocket.connect() 调用不应该socket_receiver() 协程内吗?
  • project documentation 似乎确实表明了这一点。
  • 请参阅Both pattern in the intro section of the websockets documentation,了解如何创建用于发送和接收的 websocket 连接。
  • 我在socket_receiver里面用过,但问题是一样的,而且我什至必须发送消息所以我应该在外面打开套接字
  • sys.stdout.print 不是函数,您当前正在“打印”原始 Python 对象(不需要重新编码为 JSON 吗?)。您也不能只以非阻塞方式使用sys.stdoutsys.stdin

标签: python python-3.x multithreading websocket python-asyncio


【解决方案1】:

您没有正确使用ThreadPoolExecutor,并且您真的不想在这里使用它。相反,您需要设置消费者和生产者来处理您的套接字和管道,并使用队列在它们之间发送消息。

  • 对于每种连接类型,创建一个创建连接的协程,然后将该单个连接传递给该连接的消费者和生产者任务(使用asyncio.create_task() 创建)。使用asyncio.wait()return_when=asyncio.FIRST_COMPLETED 一起运行这两个任务,因此当两个任务之一“提前”完成(例如失败)时,您可以取消任何仍在运行的任务。

  • 使用queue 将消息从一个连接的消费者传递到另一个连接的生产者。

  • sys.stdinsys.stdout阻塞 流,不要只是读写它们!有关尝试设置非阻塞 STDIO 流的要点,请参阅 https://gist.github.com/nathan-hoad/8966377,以及要求提供非阻塞流功能的 this asyncio issue

  • 不要使用全局套接字连接,当然不要使用两个单独的async with 语句。您的send_to_socket() 方法实际上会关闭 套接字,因为async with connection as web_socket: 上下文管理器在发送第一条消息时退出,这会导致socket_receiver 代码出现问题,该代码假定套接字无限期保持打开状态.

  • 不要在这里使用线程!您的连接完全由 asyncio 管理,线程将主要影响这一点。

  • asyncio.Executor() instances 只能与常规可调用对象一起使用,不能与协程一起使用。 Executor.submit() 声明它需要一个可调用的,通过 executor.submit(send_to_pipe(message))executor.submit(send_to_socket(message)) 传入协程将导致引发异常,因为协程不是可调用的。您可能没有看到异常消息,因为该异常是在另一个线程中引发的。

    这就是你的 socket_receiver() 协程失败的原因;它肯定开始,但尝试发送消息失败。当我针对本地模拟 websocket 服务器运行您的代码时,会打印一条警告:

    RuntimeWarning: coroutine 'send_to_socket' was never awaited
      executor.submit(send_to_socket(message))
    

    当没有等待协程时,该协程中的代码永远不会执行。将协程包装在一个打印出 stderr (try: callable(), except Exception: traceback.print_exc(file=sys.stderr))) 异常的协程中,您将得到:

    Traceback (most recent call last):
      File "soq52219672.py", line 15, in log_exception
        callable()
    TypeError: 'coroutine' object is not callable
    

Executors 应该只用于集成无法转换为使用协程的代码;执行程序管理该代码以与asyncio 任务并行运行而不受干扰。如果该代码想要与asyncio 任务交互,则应小心,请始终使用asyncio.run_coroutine_threadsafe()asyncio.call_soon_threadsafe() 跨边界调用。请参阅Concurrency and multithreading section

这是我如何重写代码以使用消费者/生产者模式的示例,stdio() 基于 Nathan Hoad gist on the subject,加上 Windows 的后备support for treating stdio as pipes is limited

import asyncio
import json
import os
import sys

import websockets

async def socket_consumer(socket, outgoing):
    # take messages from the web socket and push them into the queue
    async for message in socket:
        await outgoing.put(message)

async def socket_producer(socket, incoming):
    # take messages from the queue and send them to the socket
    while True:
        message = await incoming.get()
        jsonmessage = json.dumps(message)
        await socket.send(jsonmessage)

async def connect_socket(incoming, outgoing):
    header = {"Authorization": r"Basic XXXX="}
    uri = 'wss://XXXXXXXX'
    async with websockets.connect(uri, extra_headers=header) as websocket:
        # create tasks for the consumer and producer. The asyncio loop will
        # manage these independently
        consumer_task = asyncio.create_task(socket_consumer(websocket, outgoing))
        producer_task = asyncio.create_task(socket_producer(websocket, incoming))

        # start both tasks, but have the loop return to us when one of them
        # has ended. We can then cancel the remainder
        done, pending = await asyncio.wait(
            [consumer_task, producer_task],
            return_when=asyncio.FIRST_COMPLETED
        )
        for task in pending:
            task.cancel()
        # force a result check; if there was an exception it'll be re-raised
        for task in done:
            task.result()


# pipe support
async def stdio(loop=None):
    if loop is None:
        loop = asyncio.get_running_loop()

    if sys.platform == 'win32':
        # no support for asyncio stdio yet on Windows, see https://bugs.python.org/issue26832
        # use an executor to read from stdio and write to stdout
        class Win32StdinReader:
            def __init__(self):
                self.stdin = sys.stdin.buffer 
            async def readline():
                # a single call to sys.stdin.readline() is thread-safe
                return await loop.run_in_executor(None, self.stdin.readline)

        class Win32StdoutWriter:
            def __init__(self):
                self.buffer = []
                self.stdout = sys.stdout.buffer
            def write(self, data):
                self.buffer.append(data)
            async def drain(self):
                data, self.buffer = self.buffer, []
                # a single call to sys.stdout.writelines() is thread-safe
                return await loop.run_in_executor(None, sys.stdout.writelines, data)

        return Win32StdinReader(), Win32StdoutWriter()

    reader = asyncio.StreamReader()
    await loop.connect_read_pipe(
        lambda: asyncio.StreamReaderProtocol(reader),
        sys.stdin
    )

    writer_transport, writer_protocol = await loop.connect_write_pipe(
        asyncio.streams.FlowControlMixin,
        os.fdopen(sys.stdout.fileno(), 'wb')
    )
    writer = asyncio.streams.StreamWriter(writer_transport, writer_protocol, None, loop)

    return reader, writer

async def pipe_consumer(pipereader, outgoing):
    # take messages from the pipe and push them into the queue
    while True:
        message = await pipereader.readline()
        if not message:
            break
        await outgoing.put(message.decode('utf8'))

async def pipe_producer(pipewriter, incoming):
    # take messages from the queue and send them to the pipe
    while True:
        jsonmessage = await incoming.get()
        message = json.loads(jsonmessage)
        type = int(message.get('header', {}).get('messageID', -1))
        # 1 is DENM message, 2 is CAM message
        if type in {1, 2}:
            pipewriter.write(jsonmessage.encode('utf8') + b'\n')
            await pipewriter.drain()

async def connect_pipe(incoming, outgoing):
    reader, writer = await stdio()
    # create tasks for the consumer and producer. The asyncio loop will
    # manage these independently
    consumer_task = asyncio.create_task(pipe_consumer(reader, outgoing))
    producer_task = asyncio.create_task(pipe_producer(writer, incoming))

    # start both tasks, but have the loop return to us when one of them
    # has ended. We can then cancel the remainder
    done, pending = await asyncio.wait(
        [consumer_task, producer_task],
        return_when=asyncio.FIRST_COMPLETED
    )
    for task in pending:
        task.cancel()
    # force a result check; if there was an exception it'll be re-raised
    for task in done:
        task.result()

async def main():
    pipe_to_socket = asyncio.Queue()
    socket_to_pipe = asyncio.Queue()

    socket_coro = connect_socket(pipe_to_socket, socket_to_pipe)
    pipe_coro = connect_pipe(socket_to_pipe, pipe_to_socket)

    await asyncio.gather(socket_coro, pipe_coro)

if __name__ == '__main__':
    asyncio.run(main())

然后从两个任务开始,一个管理套接字,另一个管理 STDIO 管道。两者都为他们的消费者和生产者启动了另外 2 个任务。有两个队列将消息从一个的消费者发送到另一个的生产者。

【讨论】:

  • 我用新的 ubuntu 虚拟机修复了,唯一不起作用的方法是 pipe_producer,socket_consumer 接收 json 但 pipe_producer kepp on await incoming.get()
  • @luca 是在写入管道时使用缓冲的专利过程吗?确保冲洗!
  • 对象在队列中由socket_consumer(从套接字接收)但pipe_producer 没有从incoming.get() 继续,我不认为是父进程的问题
  • @luca 我无法重现这个。队列经过了很好的测试。您确定不会错过标题无效的消息的获取吗?我也没有包括 JSON 解析错误处理,添加尝试可能是谨慎的...除了那里和日志问题。
  • 我只针对这个问题开一个新问题here
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-15
  • 1970-01-01
  • 2020-04-02
  • 1970-01-01
  • 2015-10-30
  • 1970-01-01
  • 2019-02-07
相关资源
最近更新 更多