Hello,<br>
<br>
I am new to twisted and I have a few questions: I have implemented a TCPServer<br>
and the associated Factory, which manages data and dispatches treatments<br>
(According to the tutorial it's not the best solution but actually this is not<br>
the point ;) )<br>
<br>
All of the treatments run in threads and thus use shared data. The allData<br>
dictionnary is critical since it can be used by multiple threads. How can I keep <br>
the allData integrity ? Does Twisted core provide such facilites or do I<br>
have to use semaphore or other stuff ?<br>
<br>
<br>
class GestionnaireXXX(protocol.Protocol)<br>
def dataReceived(self, data):<br>
self.factory.dispatch(data)<br>
<br>
class GestionnaireXXXFactory(protocol.ServerFactory):<br>
def __init__(self):<br>
self.allData = {}<br>
self.initData()<br>
self._dispatcher = {<br>
1: self.alert,<br>
4: self.create,<br>
}<br>
def initData(self):<br>
if os.path.isfile(TEMP)<br>
self.allData = aot.unjellyFromSource(file(TEMP, 'r'))<br>
def dispatch(self, data):<br>
splitedData = data.split('\n')<br>
method = self._dispatcher.get(len(splitedData), self.doNothing)<br>
reactor.runInThread(method, splitedData)<br>
<br>
def create(self, *data):<br>
# Some treatments on allData<br>
...<br>
aot.jellyToSource(file(TEMP, 'w'))<br>
<br>
def alert(self, *unused):<br>
# XXX<br>
sem = DeferredSemaphore()<br>
sem.acquire()<br>
allDataCopy = self.allData.copy()<br>
self.allData.clear()<br>
sem.release()<br>
# End XXX<br>
# some long treatments on allDataCopy<br>
...<br>
<br>
Thank's,<br>
<br>
--<br>
Olivier Guilloux<br>
<br>