I'm trying to get pyside, multiprocessing/threading and django work together. I have a function "update_source_product" that is called upon a push button of django object actions.
class BrowserActions(object):
def update_source_product(self, request, obj):
mongo_link = Link.objects.get(_id=ObjectId(obj.source))
p = multiprocessing.Process(target=GET.main, args=(mongo_link.link, ))
p.start()
p.join()
mongo_link.save()
Following are Pyside imports
from PySide.QtGui import *
from PySide.QtCore import *
from PySide.QtWebKit import *
In the function a process is supposed to be started that shows a QApplication on the server which is defined in the following function.
class GET(object):
def __init__(self):
pass
@classmethod
def main(self, url):
app = QApplication(sys.argv)
web = QWebView()
web.load(QUrl(url))
web.show()
sys.exit(app.exec_())
Now when I run this code as a thread with
t = threading.Thread(target=GET.main, args=(mongo_link.link, ))
t.daemon = True
t.start()
t.join()
a window pops up and shows the page
but when I close the window, Windows throws an error "python.exe has stopped working"
and django dev server crashes. The stack trace is as following.
WARNING: QApplication was not created in the main() thread.
QNetworkReplyImplPrivate::error: Internal problem, this method must only be called once.
QNetworkReplyImplPrivate::error: Internal problem, this method must only be called once.
content-type missing in HTTP POST, defaulting to application/x-www-form-urlencoded. Use QNetworkRequest::setHeader() to fix this problem.
content-type missing in HTTP POST, defaulting to application/x-www-form-urlencoded. Use QNetworkRequest::setHeader() to fix this problem.
content-type missing in HTTP POST, defaulting to application/x-www-form-urlencoded. Use QNetworkRequest::setHeader() to fix this problem.
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
QObject::killTimers: timers cannot be stopped from another thread
And When I run it as a separate process with
p = multiprocessing.Process(target=GET.main, args=(mongo_link.link, ))
p.start()
p.join()
The Window never shows up but the django dev server keeps running and prints the following stack trace
File "<string>", line 1, in <module>
File "C:\Python27\Lib\multiprocessing\forking.py", line 381, in main
self = load(from_parent)
File "C:\Python27\Lib\pickle.py", line 1384, in load
return Unpickler(file).load()
File "C:\Python27\Lib\pickle.py", line 864, in load
dispatch[key](self)
File "C:\Python27\Lib\pickle.py", line 1096, in load_global
klass = self.find_class(module, name)
File "C:\Python27\Lib\pickle.py", line 1130, in find_class
__import__(module)
File "C:\Users\Snst\PycharmProjects\isp_data_core\data\tasks.py", line 1, in <module>
from data.models import Product, Pattern, Campus_Product, Campus, \
File "C:\Users\Snst\PycharmProjects\isp_data_core\data\models.py", line 2, in
<module>
from django.contrib.auth.models import User
File "C:\Users\Snst\PycharmProjects\isp_data_core\isp_data_core_venv\lib\site-packages\django\contrib\auth\models.py", line 4, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "C:\Users\Snst\PycharmProjects\isp_data_core\isp_data_core_venv\lib\site-packages\django\contrib\auth\base_user.py", line 49, in <module>
class AbstractBaseUser(models.Model):
File "C:\Users\Snst\PycharmProjects\isp_data_core\isp_data_core_venv\lib\site-packages\django\db\models\base.py", line 94, in __new__
app_config = apps.get_containing_app_config(module)
File "C:\Users\Snst\PycharmProjects\isp_data_core\isp_data_core_venv\lib\site-packages\django\apps\registry.py", line 239, in get_containing_app_config
self.check_apps_ready()
File "C:\Users\Snst\PycharmProjects\isp_data_core\isp_data_core_venv\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
Can someone please suggest a solution or point out what am I doing wrong in QApplication life-cycle as a process/thread.