-
Notifications
You must be signed in to change notification settings - Fork 273
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
testing.DaphneProcess fails when multiprocessing start method set to spawn #310
Comments
This is not a bad solution for the moment. (It's still working just fine on Python 3.7) But yes... |
After double checking, environment variables are successfully passed to the spawned process (contrary to my statement above). Also, it seems that this is likely an issue on Windows and Linux although I am unable to verify. |
I am facing the same issue of "Can't pickle local object..." in macOS Catalina.
|
I seem to have stumbled upon the solution to this problem. In PR to follow. There may be an issue, however, due to Channels passing the |
@bjd183 -- good work investigating! Look forward to the PR. Sounds fun to look at! |
Fixed in #440 |
I'm attempting to upgrade my project from Python 3.7.5 to Python 3.8.1. I'm on MacOS 10.15.3 using Python 3.7.5 and Daphne 2.4.1 with Channels 2.4.0 and Django 3.0.2. The issue manifests while invoking tests using the standard django approach:
manage.py test
.The default start method for multiprocessing on MacOS (where I perform my local test) was changed from fork to spawn in Python 3.8 due to issues documented here. The result of this change is that Python attempts to pickle DaphneProcess in order to spawn. This is true in Python 3.8 where spawn is the default and in Python 3.7 if the start method is changed from fork to spawn. The first error is due to the presence of non-picklable lambda expressions in the initializer.
This is easily remedied by replacing
lambda: None
withtype(None)
in testing.DaphneProcess.init. The next symptom is that the spawned process doesn't retain any of the Django state fromdjango.setup()
and Django warns that apps aren't loaded.It seems that because Daphne can be used without Channels/Django, calling
django.setup()
should be performed outside Daphne but I'm not sure if there is another approach or if I am missing something.I inserted the following into channels.auth:
import django
if not django.apps.apps.ready:
django.setup()
in an effort to load apps (which worked) but apparently the spawn also discarded environment variables and any information about the current test database resulting in a connection to a database without any applied migrations. Down the rabbit hole I go.
My current workaround is to call `multiprocessing.set_start_method('fork') in my django manage.py file. While this reverts to the same behavior I have by default on Python 3.7.5, I anticipate that 1) there may be hidden issues with this because Channels/Django uses threads to make database calls, and 2) fork may be deprecated in the future on MacOS. based on the python issue referenced above.
At this point it is not clear to me how much of the problem or the solution is daphne vs channels. Perhaps some of both. It occurs to me that the ability to pass environment variables through DaphneProcess may resolve the database connection issue.
The text was updated successfully, but these errors were encountered: