【发布时间】:2015-01-27 04:00:09
【问题描述】:
以下代码监听 2 端口,当有消息修改全局 dict 对象时。还有一个定时器也会修改dict。
d = {}
class x(Protocol):
def dataReceived(self, data):
# according to data call x's function
self.f()
def f(self):
global d
d['x'] = 'x'
class y(Protocol):
def dataReceived(self, data):
# according to data call y's function
self.f()
def f(self):
global d
d['y'] = 'y'
def modify_d():
global d
for k in d.keys():
if d[k] == 'whatever':
del d[k]
reactor.listenTCP(8880, x())
reactor.listenTCP(8881, y())
lc = task.LoopingCall(modify_d)
lc.start(300)
reactor.run()
访问时需要在d周围加锁吗?
【问题讨论】:
标签: python multithreading twisted