Django mod-wsgi One Class对象的多个实例

时间:2023-02-01 13:10:04

I am currently using a DJango - apache installation (on mod_wsgi) to run a website (project) which hosts an intranet cards game, whose rules are complicated enogh that it requires only 1 master list+dict data structure of scores for each user. Individual user's score can be impacted by other users actions also. I am using a Score class which I instantiate in urls.py and all players / users access this class' object [scObj = Scorer('Spades') ]. internally in this obj there are Dicts of players actions, score updates etc etc.

我目前正在使用DJango-apache安装(在mod_wsgi上)来运行一个托管内部网卡游戏的网站(项目),其规则很复杂,因为每个用户只需要1个主列表+ dict数据结构。个人用户的分数也可能受到其他用户操作的影响。我正在使用一个Score类,我在urls.py中实例化,所有玩家/用户都访问这个类'对象[scObj = Scorer('Spades')]。在这个对象内部有玩家行动,得分更新等的Dicts等。

    class Scorer:
        def __init__(self, suitname):
            self.__suitname = suitname

The scorer class is separately stored in Scorer.py

记分员类分别存储在Scorer.py中

    scObjs = (Scorer('Spades'), Scorer('Hearts'), Scorer('Clubs'), Scorer('Diamonds'),)

The objects are created in urls.py

对象在urls.py中创建

This model was running perfectly in the DEV deployement. When I moved to PROD in apache, this broke (as in there are multiple instances of scObj being created, so a score update from player-1's move on player-2 is not reflected in a different move by player-3 on player-2) . I need to simulate a singleton class behaviour / global scObj behaviour, which I am not able to do. I am running apache in worker - mpm - multithreaded mode, so I assume that the problem of multiple copies of scObj being there due to multi-process is not there.

这个模型在DEV部署中运行得很好。当我在apache中移动到PROD时,这破坏了(因为有多个scObj实例正在创建,所以玩家-1在玩家-2上移动的分数更新不会反映在玩家-3在玩家-2上的不同移动中)。我需要模拟单例类行为/全局scObj行为,这是我无法做到的。我在worker-mpm - 多线程模式下运行apache,所以我假设scObj的多个副本由于多进程而存在的问题不存在。

Is the problem related to mod_wsgi?

问题与mod_wsgi有关吗?

1 个解决方案

#1


1  

Read:

http://code.google.com/p/modwsgi/wiki/ProcessesAndThreading

Using embedded mode, whether it be worker MPM or prefork MPM on UNIX systems will be multi process.

使用嵌入式模式,无论是UNIX系统上的worker MPM还是prefork MPM都将是多进程的。

Use daemon mode and the default of a single process and you should be okay.

使用守护进程模式和单个进程的默认值,你应该没问题。

See notes at end of that documentation though about using proper database storage if necessary. Also heed warnings about multithread access to data and make code thread safe.

请参阅该文档末尾的注释,但必要时请使用正确的数据库存储。还注意有关多线程访问数据和使代码线程安全的警告。

#1


1  

Read:

http://code.google.com/p/modwsgi/wiki/ProcessesAndThreading

Using embedded mode, whether it be worker MPM or prefork MPM on UNIX systems will be multi process.

使用嵌入式模式,无论是UNIX系统上的worker MPM还是prefork MPM都将是多进程的。

Use daemon mode and the default of a single process and you should be okay.

使用守护进程模式和单个进程的默认值,你应该没问题。

See notes at end of that documentation though about using proper database storage if necessary. Also heed warnings about multithread access to data and make code thread safe.

请参阅该文档末尾的注释,但必要时请使用正确的数据库存储。还注意有关多线程访问数据和使代码线程安全的警告。