Here is my project folder:
\prjname
---\appname
---\---\subappname1
---\---\---\__init.py
---\---\subappname2
---\---\---\__init.py
---\---\view.py
---\---\models.py
---\---\admin.py
---\---\__init__.py
In models.py I defined some model like:
class model1(models.Model) :
name=models.CharField(primary_key=True, max_length=32)
In subappname1.init.py I have some code like:
from appname.models import model1
import multiprocessing
class myclass(Object):
def myfun(self):
res = []
threadargs=[(arg1,arg2),(arg3,arg4)]
pool = multiprocessing.Pool(processes=multiprocessing.cpu_count())
for arg in threadargs:
res.append(pool.apply_async(self.mywork,arg))
pool.close()
pool.join()
for re in res:
re = re.get()
self.myrecord(re)
def mywork(self,arg1,arg2):
#do something
pass
def myrecord(self,re):
model2.object.create(name=re)
I think it is easy to understand. I am trying to do some time-consuming work by multiprocess. However, when I run such code, I got error below:
Process SpawnPoolWorker-2:
Traceback (most recent call last):
File "E:\programfile\python\lib\multiprocessing\process.py", line 249, in _bootstrap
self.run()
File "E:\programfile\python\lib\multiprocessing\process.py", line 93, in run
self._target(*self._args, **self._kwargs)
File "E:\programfile\python\lib\multiprocessing\pool.py", line 108, in worker
task = get()
File "E:\programfile\python\lib\multiprocessing\queues.py", line 345, in get
return _ForkingPickler.loads(res)
File "E:/suilong/workspace/myproject\myappname\subappname1\__init__.py", line 7, in <module>
from myappname.models import models
File "E:/suilong/workspace/myproject\myappname\models.py", line 10, in <module>
class model1(models.Model):
File "E:\programfile\python\lib\site-packages\django\db\models\base.py", line 105, in __new__
app_config = apps.get_containing_app_config(module)
File "E:\programfile\python\lib\site-packages\django\apps\registry.py", line 237, in get_containing_app_config
self.check_apps_ready()
File "E:\programfile\python\lib\site-packages\django\apps\registry.py", line 124, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
Here is my information:
OS: windows7
python: python3.6.0
django: 1.10.6
datebase: Slite3
Is there anyone who can help me? Thanks a lot.