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
"DatabaseWrapper objects created in a thread can only be used in that same thread" using prefork and the celery command #3520
Comments
Can you try installing celery 4.0rc5 ( Usually we've seen this when the gevent/eventlet monkey patches are applied too late, not with the prefork pool. I'm guessing somehow some state for the databases is initialized in the parent process, that's inherited in the child, but Celery will close the connection after fork. You mention that you don't use manage.py celery, do you use |
Not easily, because last I checked a lot of my project is incompatible with the changes announced for Celery 4 (I'm using a lot of the parts that have been deprecated). I'll give it a try on the weekend, though.
That's what I've learned, so I'm really trying to understand what I'm doing differently to cause this in prefork right now.
Sorry, I don't quite understand. Are you saying initializing the database in the parent process and then closing it after the fork is what's supposed to happen, or are you saying that's what should not happen, but if it were to happen, it might cause my issue? Because that's what I believe I'm seeing in debugging. However, note that I haven't found a way to debug the actual The problem is that I haven't found any documentation about how the patch is supposed to work: Is the
I wasn't. I've added it now to the very end of my |
I have resolved this issue. Another library was the culprit, it wasn't caused by celery or django-celery. I'll add details later. |
I could eventually narrow this down to the following lines in the devourer library:
Commenting these out (remember, I'm not using gevent in celery at all, just prefork) fixed all my issues. So the debugging advice I have for everyone reading this due to a similar issue is: even if the stack trace looks like celery, the issue might not originate in celery (thanks to monkey patching hell…)! Check your other libraries and see what kind of patching they might perform that could cause this. Thanks for your response, @ask, and sorry for the distraction. |
celery 3.1.24, django-celery (3.1.17), using the 'django' broker transport.
I'm launching my app with the celery command, like this:
celery -A myapp worker --pool=prefork
And I get the following exception when running a task that accesses the database (single database, standard Django ORM, latest Postgres with psycopg2):
Any idea what might cause this? I've debugged all my way through, but I'm struggling to understand how this is supposed to work when celery preforks the workers. I assume the problem is that the workers inherit the DatabaseWrapper during the fork?
This started to appear around the time I was upgrading Django from 1.8 to 1.9, but there's a lot of related changes and I cannot narrow it down to that specifically.
Related issues:
manage.py celery
to thecelery
command, which I'm already usingThe text was updated successfully, but these errors were encountered: