linux - Python 3 asyncio: run_until_complete() blocks when waiting for ProcessPoolExecutor job done -


i'm trying combine tcp echo client , server testing automation single module using processpoolexecutor() , works expected.

the issue can't finish event loop. can see debug output of last line of executor's target run_client(), looks executor still blocks.

the code:

import asyncio import concurrent.futures concurrent.futures import processpoolexecutor  async def server_handle_echo(reader, writer):     data = await reader.read(100)     message = data.decode()     addr = writer.get_extra_info('peername')     print("received %r %r" % (message, addr))      print("send: %r" % message)     writer.write(data)     await writer.drain()      print("close client socket")     writer.close()   async def echo_client_handler(message, loop):     reader, writer = await asyncio.open_connection('127.0.0.1', 8888,                                                    loop=loop)      print('send: %r' % message)     writer.write(message.encode())      data = await reader.read(100)     print('received: %r' % data.decode())      print('close socket')     writer.close()    def run_client():     message = 'hello world!'     loop = asyncio.get_event_loop()     loop.run_until_complete(echo_client_handler(message, loop))     loop.close()     print('run_client last line')  executor = processpoolexecutor(1) loop = asyncio.get_event_loop()  coro = asyncio.start_server(server_handle_echo, '127.0.0.1', 8888, loop=loop) server = loop.run_until_complete(coro)  # serve requests until ctrl+c pressed print('serving on {}'.format(server.sockets[0].getsockname())) try:     #loop.run_forever()     client = asyncio.ensure_future(loop.run_in_executor(executor, run_client))     loop.run_until_complete(client)  except keyboardinterrupt:     pass  # close server server.close() loop.run_until_complete(server.wait_closed()) loop.close() 

output:

serving on ('127.0.0.1', 8888) send: 'hello world!' received 'hello world!' ('127.0.0.1', 51157) send: 'hello world!' close client socket received: 'hello world!' close socket run_client last line 

after output hands in msg loop waiting io.

looking forward help. sorry, i'm one-day-asyncioist :)

you can't use same event loop client running in subprocess, need new loop:

def run_client():     loop = asyncio.new_event_loop()     asyncio.set_event_loop(loop)     [...] 

Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -