你认为可以在django中使用asyncio每n秒运行一次任务,这样主进程就不会被阻塞吗?
例如,在控制台中每 5 分钟打印一次,例如:
import asyncio
from random import randint
async def do_stuff(something, howmany):
for i in range(howmany):
print('We are doing {}'.format(something))
await asyncio.sleep(randint(0, 5))
if __name__ == '__main__':
loop = asyncio.get_event_loop()
work = [
asyncio.ensure_future(do_stuff('something', 5)),
]
loop.run_until_complete(asyncio.gather(*work))
循环运行时,django 似乎会停止工作。即使这可以在开发中工作,当站点在 apache 或 gunicorn 上运行时它会如何表现?