简体   繁体   English

与python3 asyncio建立联系

[英]Making connections with python3 asyncio

I'm trying to connect to more than one server at the same time. 我正在尝试同时连接到多个服务器。 I am currently using loop.create_connection but it freezes up at the first non-responding server. 我目前正在使用loop.create_connection但它在第一个无响应的服务器上冻结。

gsock = loop.create_connection(lambda: opensock(sid), server, port)
transport, protocol = loop.run_until_complete(gsock)

I tried threading this but it created problems with the sid value being used as well as various errors such as RuntimeError: Event loop is running and RuntimeError: Event loop stopped before Future completed . 我尝试线程化这个但是它产生了使用sid值的问题以及各种错误,例如RuntimeError: Event loop is runningRuntimeError: Event loop stopped before Future completed Also, according my variables (tho were getting mixed up) the protocol's connection_made() method gets executed when transport, protocol = loop.run_until_complete(gsock) throws an exception. 此外,根据我的变量(tho正在混淆)协议的connection_made()方法在transport, protocol = loop.run_until_complete(gsock)抛出异常时执行。

I don't understand much about the asyncio module so please be as thorough as possible. 我对asyncio模块了解不多,所以请尽可能详尽。 I dont think I need reader/writer variables, as the reading should be done automatically and trigger data_received() method. 我不认为我需要读/写变量,因为读取应该自动完成并触发data_received()方法。

Thank You. 谢谢。

You can connect to many servers at the same time by scheduling all the coroutines concurrently, rather than using loop.run_until_complete to make each connection individually. 您可以通过同时调度所有协程来同时连接到多个服务器,而不是使用loop.run_until_complete来单独建立每个连接。 One way to do that is to use asyncio.gather to schedule them all and wait for each to finish: 一种方法是使用asyncio.gather来安排所有这些并等待每个完成:

import asyncio

# define opensock somewhere

@asyncio.coroutine
def connect_serv(server, port):
    try:
        transport, protocol = yield from loop.create_connection(lambda: opensock(sid), server, port)
    except Exception:
        print("Connection to {}:{} failed".format(server, port))

loop = asyncio.get_event_loop()
loop.run_until_complete(
    asyncio.gather(
      connect_serv('1.2.3.4', 3333),
      connect_serv('2.3.4.5', 5555),
      connect_serv('google.com', 80),
 ))
loop.run_forever()

This will kick off all three coroutines listed in the call to gather concurrently, so that if one of them hangs, the others won't be affected; 这将启动呼叫中列出的所有三个协同程序同时gather ,这样如果其中一个挂起,其他协议将不会受到影响; they'll be able to carry on with their work while the other connection hangs. 当其他连接挂起时,他们将能够继续他们的工作。 Then, if all of them complete, loop.run_forever() gets executed, which will allow you program to continue running until you stop the loop or kill the program. 然后,如果所有这些都完成, loop.run_forever()执行loop.run_forever() ,这将允许程序继续运行,直到您停止循环或终止程序。

The reader / writer variables you mentioned would only be relevant if you used asyncio.open_connection to connect to the servers, rather than create_connection . reader / writer你提到的变量,如果你使用的只会是相关asyncio.open_connection连接到服务器,而不是create_connection It uses the Stream API, which is a higher-level API than the protocol/transport-based API that create_connection uses. 它使用Stream API,它是比create_connection使用的基于协议/传输的API更高级的API。 It's really up to you to decide which you prefer to use. 这取决于您决定使用哪种。 There are examples of both in the asyncio docs, if you want to see a comparison. 如果您想查看比较,那么asyncio文档中都有两个 示例

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

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