【发布时间】:2016-10-17 07:55:44
【问题描述】:
我正在尝试在金字塔视图中运行 asyncio 子进程,但视图挂起并且异步任务似乎永远无法完成。我可以在金字塔视图之外运行这个示例,它可以工作。
话虽如此,我最初使用loop = asyncio.get_event_loop() 进行了测试,但这告诉我RuntimeError: There is no current event loop in thread 'Dummy-2'
这里肯定有一些我不完全理解的东西。就像视图线程可能与主线程不同,所以get_event_loop 不起作用。
那么有人知道为什么我的异步任务在这种情况下可能不会产生结果吗?这是一个幼稚的例子。
@asyncio.coroutine
def async_task(dir):
# This task can be of varying length for each handled directory
print("Async task start")
create = asyncio.create_subprocess_exec(
'ls',
'-l',
dir,
stdout=asyncio.subprocess.PIPE)
proc = yield from create
# Wait for the subprocess exit
data = yield from proc.stdout.read()
exitcode = yield from proc.wait()
return (exitcode, data)
@view_config(
route_name='test_async',
request_method='GET',
renderer='json'
)
def test_async(request):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
dirs = ['/tmp/1/', '/tmp/2/', '/tmp/3/']
tasks = []
for dir in dirs:
tasks.append(asyncio.ensure_future(async_task(dir), loop=loop))
loop.run_until_complete(asyncio.gather(*tasks))
loop.close()
return
【问题讨论】:
-
Pyramid 框架与 asyncio 不兼容,没有理由一起使用。
-
我有一种感觉,可能是这样的。我还是有兴趣为什么?我注意到 uWSGI 服务器有一个使用 asyncio 的实验性功能,这会改变它的行为吗?
-
没有。 Pyramid 是一个 WSGI 框架。 WSGI 按照标准定义是同步的。
标签: pyramid python-3.5 python-asyncio