2015-03-03 27 views
2
>>> import asyncio 
>>> help(asyncio.wait) 

..ayncio超时解释

Help on function wait in module asyncio.tasks: 

wait(fs, *, loop=None, timeout=None, return_when='ALL_COMPLETED') 
    Wait for the Futures and coroutines given by fs to complete. 

    Coroutines will be wrapped in Tasks. 

    Returns two sets of Future: (done, pending). 

    Usage: 

     done, pending = yield from asyncio.wait(fs) 

    Note: This does not raise TimeoutError! Futures that aren't done 
    when the timeout occurs are returned in the second set. 
(END) 

我不很明白最后一个音符在此帮助(什么是第二套?是它挂起/后处理组?我如何执行尚未完成的任务和结合完成和待处理的结果,然后保存在数据库中)

我的问题: 我与aiohttp使用asyncio,有数以百万计的网址,他们很少可能会引发超时错误。我想将它们发送到队列中进行重新处理,或者应该通过事件池进行处理。 PS:我不使用wait_for方法。

回答

3

下面是从帮助两套:

Returns two sets of Future: (done, pending). 

第二组是pending集,还没有超时内完成工作。它会返回一个包含两个期货清单的元组,其中一个是已完成的,另一个尚未完成。

代替:

c = asyncio.wait([process_data(url) for url in url_list], timeout=10) 
loop.run_until_complete(c) 

你应该:

def dostuff(): 
    done, pending = yield from asyncio.wait([process_data(url) for url in url_list], timeout=10) 

    # do something with pending 

loop.run_until_complete(dostuff()) 

下面是详细信息:

https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.wait

+0

我想运行尚未完成的任务以及(异步)。我怎么能再次通过event_loop)? – micheal 2015-03-04 06:43:15

+0

@michael在等待超时后,待处理任务继续执行。它们不会被取消或中止或类似的事情。因此,无需重新安排它们的操作 - 只要您不停止事件循环,它们将继续在后台运行。 – dano 2015-03-17 20:17:00