[Twisted-Python] How can I keep data integrity in multiple threads environment
Kaladan
olivier.guilloux at gmail.com
Mon Dec 12 10:04:07 EST 2005
Hello,
I am new to twisted and I have a few questions: I have implemented a
TCPServer
and the associated Factory, which manages data and dispatches treatments
(According to the tutorial it's not the best solution but actually this is
not
the point ;) )
All of the treatments run in threads and thus use shared data. The allData
dictionnary is critical since it can be used by multiple threads. How can I
keep
the allData integrity ? Does Twisted core provide such facilites or do I
have to use semaphore or other stuff ?
class GestionnaireXXX(protocol.Protocol)
def dataReceived(self, data):
self.factory.dispatch(data)
class GestionnaireXXXFactory(protocol.ServerFactory):
def __init__(self):
self.allData = {}
self.initData()
self._dispatcher = {
1: self.alert,
4: self.create,
}
def initData(self):
if os.path.isfile(TEMP)
self.allData = aot.unjellyFromSource(file(TEMP, 'r'))
def dispatch(self, data):
splitedData = data.split('\n')
method = self._dispatcher.get(len(splitedData), self.doNothing)
reactor.runInThread(method, splitedData)
def create(self, *data):
# Some treatments on allData
...
aot.jellyToSource(file(TEMP, 'w'))
def alert(self, *unused):
# XXX
sem = DeferredSemaphore()
sem.acquire()
allDataCopy = self.allData.copy()
self.allData.clear()
sem.release()
# End XXX
# some long treatments on allDataCopy
...
Thank's,
--
Olivier Guilloux
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20051212/94f8a4b6/attachment.htm
More information about the Twisted-Python
mailing list