这是一个奇怪的错误,我在尝试使用“asyncio”来安排“mongoimport”任务时遇到了。一旦我启动了mongod服务,并粘贴了我在终端上生成的命令,就可以了。但是,当我尝试使用python3.4 asyncio时,问题出现了:
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是一个轻量包装器,在run_util_complete
(asyncio.gather(* targets))之后有一个timmer。目标定义为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()
答案 0 :(得分:0)
现在我更改了代码
$sNumber = 'a24432';
$sAllowedValues = "/^\+?[0-9 EeXxTt]*$/";
$res = preg_match($sAllowedValues, $sNumber, $sMatches);
var_dump($res); // 1 - OK, 0 - there are invalid chars
并且最终感谢重新排列代码结构。 THX !!!
...
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()