Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 协程从来没有被等待过,异步多线程_Python 3.x_Python Asyncio_Python Multithreading_Aiohttp - Fatal编程技术网

Python 3.x 协程从来没有被等待过,异步多线程

Python 3.x 协程从来没有被等待过,异步多线程,python-3.x,python-asyncio,python-multithreading,aiohttp,Python 3.x,Python Asyncio,Python Multithreading,Aiohttp,我还是一个python初学者,我真的不知道应该在这里修改什么才能得到我想要的结果。 非常感谢您的帮助,如果这是一个重复的问题,我很抱歉。由于existance是异步的,您根本不需要线程,因此您可以像下面这样实现进程: RuntimeWarning: coroutine 'existance' was never awaited future = asyncio.ensure_future(process(names)) async def process(names): async

我还是一个python初学者,我真的不知道应该在这里修改什么才能得到我想要的结果。
非常感谢您的帮助,如果这是一个重复的问题,我很抱歉。

由于
existance
是异步的,您根本不需要线程,因此您可以像下面这样实现
进程

RuntimeWarning: coroutine 'existance' was never awaited
  future = asyncio.ensure_future(process(names))
async def process(names):
    async with aiohttp.ClientSession() as s:
        tasks = []
        for name in names:
            if len(name) >= 5 and len(name) < 16 and name.isalnum():
                tasks.append(existance(s, name))
        return await asyncio.gather(*tasks)
但是不要试图通过
run\u-in\u-executor
调用异步函数,这是行不通的。

不需要协同路由,它需要正常的函数。您试图以一种不起作用的方式混合使用多线程和异步IO。您应该使用线程池来执行阻塞IO,或者使用异步IO和协同路由。
async def process(names):
    async with aiohttp.ClientSession() as s:
        tasks = []
        for name in names:
            if len(name) >= 5 and len(name) < 16 and name.isalnum():
                tasks.append(existance(s, name))
        return await asyncio.gather(*tasks)
# instead of:
# result = slow_calculation(arg1, arg2)   # sync/slow code
# use this:
loop = asyncio.get_event_loop()
result = await loop.run_in_executor(None, slow_calculation, arg1, arg2)