当尝试安排“mongoimport”任务时raise raise RunError('Event loop is closed')
问题描述:
这是一个奇怪的错误,我尝试使用“asyncio”安排“mongoimport”任务时遇到。一旦我启动了mongod服务,并在终端上生成了粘贴的命令,它就起作用了。然而,当我尝试使用python3.4 ASYNCIO,问题来了:当尝试安排“mongoimport”任务时raise raise RunError('Event loop is closed')
File "/Users/wangyi/Documents/workspace/Math/machine_learning/ditech/io/collect.py", line 100, in <module>
parse_train_data()
File "/Users/wangyi/Documents/workspace/Math/machine_learning/ditech/io/collect.py", line 95, in parse_train_data
call_in_background(*targets)
File "/Users/wangyi/Documents/workspace/Math/machine_learning/ditech/io/collect.py", line 19, in wrapper
result = func(*args, **keywords)
File "/Users/wangyi/Documents/workspace/Math/machine_learning/ditech/io/collect.py", line 41, in call_in_background
loop.run_until_complete(asyncio.gather(*targets, loop=loop))
File "/usr/local/lib/python3.4/asyncio/tasks.py", line 567, in gather
fut = async(arg, loop=loop)
File "/usr/local/lib/python3.4/asyncio/tasks.py", line 511, in async
task = loop.create_task(coro_or_future)
File "/usr/local/lib/python3.4/asyncio/base_events.py", line 211, in create_task
self._check_closed()
File "/usr/local/lib/python3.4/asyncio/base_events.py", line 265, in _check_closed
raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
这里是我的代码片段:
def parse_train_data():
commandtpl = "mongoimport --host=127.0.0.1 -d DiDitech -c {table} -f {fields} --type tsv --file {target}"
for tb in tables:
tasks = list(map(lambda t: commandtpl.format(table=tb, fields=','.join(fields[tb]), target=t), train_dest[tb]))
print('commands of %s:' % tb)
print('' + '\n'.join(tasks))
targets = [get_lines(ob) for ob in tasks]
call_in_background(*targets)
call_in_background时(asyncio.gather的run_util_complete
轻包装用蒂默( *目标))。目标定义为PEP中所述的coros。
def call_in_background(*targets):
...
loop = get_loop()
print(loop.run_until_complete(asyncio.gather(*targets, loop=loop, return_exceptions=True)))
#loop.close()
@asyncio.coroutine
def get_lines(shell_command):
task = yield from asyncio.create_subprocess_shell(shell_command,
stdin=PIPE, stdout=PIPE, stderr=STDOUT)
return (yield from task.communicate())[0].splitlines()
答
现在我改变了代码
...
flag = False
if loop is None:
loop = get_loop()
flag = True
print(loop.run_until_complete(asyncio.gather(*targets, loop=loop, return_exceptions=True)))
if flag == False:
loop.close()
,并与重排码结构终于作品的感谢。谢谢!!!
2016-06-28T16:39:59.128+0800 connected to: 127.0.0.1
2016-06-28T16:39:59.136+0800 imported 279 documents
2016-06-28T16:39:59.130+0800 connected to: 127.0.0.1
2016-06-28T16:39:59.145+0800 imported 66 documents
2016-06-28T16:39:59.134+0800 connected to: 127.0.0.1
2016-06-28T16:39:59.142+0800 imported 288 documents
2016-06-28T16:39:59.140+0800 connected to: 127.0.0.1
2016-06-28T16:39:59.148+0800 imported 285 documents
'call_in_background'是什么样子的?它是否关闭了循环?请编辑该问题以包含[最小,完整和可验证示例](https://*.com/help/mcve)。 – dirn
@dirn yes一旦run_until_complete返回,它将关闭循环。我应该打电话给asyncio Anrew的作者寻求帮助吗? –
这就是你的问题。在完成之前不要关闭循环。 – dirn