简体   繁体   中英

Python data persistent object for communication between threads

As shown here , I set up a python Django application served by cherrypy wsgi server. The app is basically another IRC client. Here's the deal : it is very likely that I have to create several separate instances of my bot for every new server connection I need to establish. I need to be able to communicate with each bot. One suggested answer was to use multiprocessing.Queue . This object is data persistent and does allow me to communicate with a bot. However, I need all my bots to listen to the same signal simultaneously, to stop for instance. Every bot has to control whether the stop signal was for him or not. Therefore, I need an object, or some other method, that allows for each bot, running in a separate daemonic thread, to listen to a bunch of signals. The Bus system used by cherrypy for server wide messages is great but seems like an overkill here and I would have no idea how to implement it. Any suggestions ?

So, turns out I just mixed up variables and objects. Variables cannot be defined in one module and imported to another one (a = foo, then modify a from another module and import a from third module). This is possible with lists, dicts and stuff. Solution : at each bot launch (and attempt to connect to a given server), a queue is created and assigned to this server in a dict with a key : the nickname (that has to be unique on IRC). Each bot has a deamonic listener that reads the queue. Other modules import the dict, the specific queue, put a signal there, that is used by the listener. The system works perfectly. Thumbs up. I'm having difficulties with one aspect though : each bot can have multiples DCC connections and it's hard to assign data to each connection and not mix them up.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM