django启动代码只执行一次 [英] django startup code executed only once

查看:546
本文介绍了django启动代码只执行一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有很多问题回答了这个话题,但没有一个是有效的。
使用 apache2.2 mod_wsgi 3.3 python 2.7.3 django 1.4 centos6.2



尝试启动我的代码形式urls.py,它是执行随机的次数,并在启动完成后随机执行。接下来,我尝试使用这里解释的中间件技巧在哪里放置Django启动代码?,启动代码也被执行随机的次数(打印出一些调试消息,多次看到相同的消息)。



问题是,我希望有一个全局TCP客户端,连接和与TCP服务器通信。它应该只被初始化一次,如果它随机创建新的实例旧的连接将丢失,销毁谁知道什么时候,该套接字可能会被破坏,当os用完内存(不确定)。所以目标是有一个全局TCP Client ,被触发TCP客户端功能的所有功能使用,例如连接/断开/发送/接收



是否有某种apache或mod_wsgi选项?我在Windows 7上运行相同的配置,一切都没有问题。

解决方案

如何修改apache httpd.conf :
WSGIDaemonProcess site-1 threads = 15



WSGIProcessGroup site-1



基本上django正在多个进程和线程中运行。为了使全局TCP客户端线程安全,锁定对象 threading.Lock()是有帮助的。也创建了TcpClient作为单例,以防万一。


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.

解决方案

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.

这篇关于django启动代码只执行一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆