Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Django Celery Gevent SynchronousOnlyOperation #72

Open
zN3utr4l opened this issue May 25, 2023 · 2 comments
Open

Django Celery Gevent SynchronousOnlyOperation #72

zN3utr4l opened this issue May 25, 2023 · 2 comments

Comments

@zN3utr4l
Copy link

I opened an issue about a library I’m using for my django project, here’s the link: Async DataBaseHandler (Sink?) Django · Issue #879 · Delgan/loguru · GitHub 2
Searching the internet I found a question on stackoverflow which describes exactly what happens: django - SynchronousOnlyOperation from celery task using gevent execution pool - Stack Overflow 1

commands:

celery==5.2.7
django==3.2.19

celery -A scheduler.celery beat -l INFO -f $path/celery/beat.log --uid=nobody --gid=nogroup --detach
celery -A scheduler.celery worker -l INFO -f $path/celery/worker.log --uid=nobody --gid=nogroup -O fair --pool=gevent --hostname=celery-workerAnalyser --autoscale=30,10 --without-heartbeat --without-gossip # --without-mingle

The main question is: But why isn’t this exception raised in 100% of cases but only in a minority of cases?

I figured out that django orm is not greenlet thread safe for gevent.

I already have a PG pooler, pgbouncer, so I would like to use its pooling, but I would like to understand if this library could help me with the error that celery throws when trying to query.

I already tried this comment: #61 (comment), but nothing changes.

@jneight
Copy link
Owner

jneight commented Jul 19, 2023

How are you initializing gevent? using a monkey patch like the one described here: https://github.com/jneight/django-db-geventpool#patch-psycopg2

@zN3utr4l
Copy link
Author

zN3utr4l commented Jul 21, 2023

yes i tried this

Patch psycopg2
Before using the pool, psycopg2 must be patched with psycogreen, if you are using gunicorn webserver, a good place is the post_fork() function at the config file:

from psycogreen.gevent import patch_psycopg     # use this if you use gevent workers

def post_fork(server, worker):
    patch_psycopg()
    worker.log.info("Made Psycopg2 Green")

In this case I was using celery, I did the same thing with celery, celeryd-init and worker_init signals (https://docs.celeryq.dev/en/stable/userguide/signals.html#signals)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants