Created
July 31, 2018 09:33
-
-
Save entwanne/819db854ea3e01f6e54968bab9ecc723 to your computer and use it in GitHub Desktop.
Celery warm shutdown solo taskpool
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import time | |
from celery import Celery | |
app = Celery('tasks') | |
class TaskBackend: | |
async def __aenter__(self): | |
self.process = await asyncio.create_subprocess_exec( | |
'celery', 'worker', '--app', 'tasks', | |
'--concurrency', '1', | |
'--pool', 'solo', | |
) | |
async def __aexit__(self, exc_type, exc, tb): | |
self.process.terminate() | |
await self.process.wait() | |
@app.task | |
def mytask(): | |
print('starting...') | |
time.sleep(5) | |
print('done') | |
async def test(): | |
async with TaskBackend(): | |
mytask.delay() | |
await asyncio.sleep(3) | |
if __name__ == '__main__': | |
loop = asyncio.get_event_loop() | |
loop.run_until_complete(test()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment