Skip to content

Instantly share code, notes, and snippets.

@entwanne
Created July 31, 2018 09:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save entwanne/819db854ea3e01f6e54968bab9ecc723 to your computer and use it in GitHub Desktop.
Save entwanne/819db854ea3e01f6e54968bab9ecc723 to your computer and use it in GitHub Desktop.
Celery warm shutdown solo taskpool
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