1
您认为可以使用asyncio在django中每n秒运行一次任务,以便主进程不会被阻塞?使用asyncio在django中执行周期性任务
的东西,例如,将打印每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之类的网站上运行时它的行为如何?
也许使用多处理将是一种替代? – Mikael