简体   繁体   中英

Django - Celery catch(try-except block) errors - issues with freezing/hang code

I'm using PythonDjango with Celery and Redis and I want to catch two erros:

  1. The first error OperationalError means the server is down
  2. If app.control.inspect().active() is None, the workers are down

Functionality:

  1. If there is not an OperationalError it should check also app.control.inspect().active()
  2. If there is an OperationalError it shouldn't check app.control.inspect().active() because Celery will hang/freeze
  3. The Celery task will not work in both cases but just the Redis server down will have an exception

Code:

   if not error:
        try:
          send_email_task.delay(subject=subject, ..., html_content=html_content)
        except OperationalError as e:
            # do something
        if not app.control.inspect().active():
                # do something   
     else:
     ......

My issue, if I have an OperationalError the condition if not app.control.inspect().active() will hang the code

If I add the code app.control.inspect().active() inside try will hang/freeze, will indefinitely check status if is an OperationalError .

Looks like you need the else block in try-catch

Try:

if not error:    
    try:
      send_email_task.delay(subject=subject, ..., html_content=html_content)
    except OperationalError as e:
        # do something
    else:
        if not app.control.inspect().active():
            # do something   
else:
    ......

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM