import os, sys, time from threading import Thread from threading import currentThread import SimpleXMLRPCServer servAddr = ("localhost", 8000) serv = SimpleXMLRPCServer.SimpleXMLRPCServer(servAddr) tt = [] import SimpleXMLRPCServer class myThread(Thread): def __init__ (self,p): self.p = p Thread.__init__(self) def run (self): t = currentThread() while 1: n = random.random() tt[self.p] = self.p + '!!!' time.sleep(n) def rn(): mythreads = [] for p in (1,2,3): t = myThread(p) mythreads.append(t) t.start() return 1 def test(): return tt serv.register_function(rn) serv.register_function(test) serv.register_introspection_functions() Add a comment |
2 Answers
Python objects like dict are already thread safe, so in that sense your script is already thread safe. What other specific thing you want to make thread safe, at-least for now it looks ok
2 Comments
Bdfy
may need to lock tt for writing or not ? docs.python.org/release/2.5.2/lib/condition-objects.html
Anurag Uniyal
@Bdfy , no objects like dict are already threadsafe for such operation, if you were doing some complex calculations involving multiple objects then you should think about locks, semaphores etc
I am not really familiar with python, but can't you use Semaphores / Monitors for atomic insurance?
1 Comment
Luc
... that's exactly what I would expect an answer here to tell me.