django startup code executed only once

非 Y 不嫁゛ 提交于 2019-12-11 00:17:24

问题


I know there have been many questions answered on this topic, but none of them worked. Using apache2.2, mod_wsgi 3.3, python 2.7.3 and django 1.4 on centos6.2.

I tried starting up my code form urls.py, it is executed random amount of times and is executed randomly after startup is already finished. Next I tried using middleware trick explained here Where to put Django startup code? ,startup code was also executed random amount of times (printing out some debug messages, seeing same message multiple times).

The problem is, I wish to have one global TCP Client, connecting and communicating with TCP Server. It should be Initialized only once, if it randomly creates new instance old connection will be lost, destroyed who knows when and that socket will probably be destroyed when os runs out of memory (not sure about that). So the goal would be to have one global TCP Client, being used by all functions that trigger TCP Client functions such as Connect/Disconnect/Send/Receive.

Is there some kind of apache or mod_wsgi option for that? I am running same configuration on Windows 7 and everything works without a problem.


回答1:


Had to modify apache httpd.conf with following lines: WSGIDaemonProcess site-1 threads=15

WSGIProcessGroup site-1

Basicly django was running in multiple processes and threads. To make global TCP Client thread safe, lock object threading.Lock() was helpful. Also created TcpClient as singleton, just in case.



来源:https://stackoverflow.com/questions/12419403/django-startup-code-executed-only-once

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!