0

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.

lanyuesl
  • 1
  • 2
  • 2
    Why do u want to write a function in init.py? Check Q similar to yours http://stackoverflow.com/questions/34114427/django-upgrading-to-1-9-error-appregistrynotready-apps-arent-loaded-yet & https://github.com/etianen/django-reversion/issues/481 – Abijith Mg Mar 21 '17 at 07:51

1 Answers1

0

You are importing model1 in __init__.py.But donno where you are using it. From django 1.9

All models need to be defined inside an installed application or declare an explicit app_label. Furthermore, it isn’t possible to import them before their application is loaded. In particular, it isn’t possible to import models inside the root package of an application.

But you can do inline import like.

def mywork(self,arg1,arg2):
    from appname.models import model1
itzMEonTV
  • 19,851
  • 4
  • 39
  • 49
  • I did as you told me. Then I got a new error: *AttributeError:'myclass' object has no attribute 'mywork'* – lanyuesl Mar 22 '17 at 11:57
  • I did as you told me. Then I got a new error: File "E:\programfile\python\lib\multiporcessing\process.py", line 249, in _bootstrap self.run() File "E:\programfile\python\lib\multiporcessing\process.py", line 93, in run self._target(*self._args,**self._kwargs) File "E:\programfile\python\lib\multiporcessing\pool.py", line 108, in worker task = get() File "E:\programfile\python\lib\multiporcessing\queues.py", line 345,in get return_ForkingPickler.loads(res) AttributeError:'myclass' object has no attribute 'mywork' – lanyuesl Mar 22 '17 at 12:34
  • There are some other things which is not posted here. First of all `Object` is inherited in `class myclass` which is not defined – itzMEonTV Mar 22 '17 at 12:43