Celery Worker Database Connection Pooling

前端 未结 6 1027
萌比男神i
萌比男神i 2020-12-07 19:17

I am using Celery standalone (not within Django). I am planning to have one worker task type running on multiple physical machines. The task does the following

    <
6条回答
  •  南笙
    南笙 (楼主)
    2020-12-07 19:53

    I like tigeronk2's idea of one connection per worker. As he says, Celery maintains its own pool of workers so there really isn't a need for a separate database connection pool. The Celery Signal docs explain how to do custom initialization when a worker is created so I added the following code to my tasks.py and it seems to work exactly like you would expect. I was even able to close the connections when the workers are shutdown:

    from celery.signals import worker_process_init, worker_process_shutdown
    
    db_conn = None
    
    @worker_process_init.connect
    def init_worker(**kwargs):
        global db_conn
        print('Initializing database connection for worker.')
        db_conn = db.connect(DB_CONNECT_STRING)
    
    
    @worker_process_shutdown.connect
    def shutdown_worker(**kwargs):
        global db_conn
        if db_conn:
            print('Closing database connectionn for worker.')
            db_conn.close()
    

提交回复
热议问题