For testing purposes you can run django development server in single-threaded mode: python manage.py runserver --nothreading
.
You want to import theano
only in celery worker process, not in django web server process, right? Ok, let's make import conditional, so that it is imported in celery and not in django.
import os
try:
# next line will raise exception in django, but will work fine in celery
is_worker = os.environ['celery_worker']
import theano # celery will import theano, django won't
except Exception as exc:
# django code will catch exception that celery_worker doesn't exist and print it here
print exc
And start your celery worker with celery_worker
environment variable set:
celery_worker=yes celery -A celery_try worker -l info
To discriminate between celery worker and django, let's set a bash environment variable in celery process, but not in django process. I called that variable celery_worker
. In order to set it, I prepended celery -A celery_try worker -l info
with per-command env variable assignment: celery_worker=yes
. Now, in python code, I check if the environment variable is present. If it is, we are in celery worker and need to import theano.
If we're in django, os.environ['celery_worker']
shouldn't be defined and should raise an exception.