在Python中添加不适用于集合的函数
我试图在一个集合中添加函数的引用(在exposed_setCallback方法中)。 答案在最后给出。不知何故,它并没有为第二次尝试添加引用。源文件的链接包括: 代码如下:在Python中添加不适用于集合的函数,python,set,add,Python,Set,Add,我试图在一个集合中添加函数的引用(在exposed_setCallback方法中)。 答案在最后给出。不知何故,它并没有为第二次尝试添加引用。源文件的链接包括: 代码如下: import rpyc test = ['hi'] myReferences= set() class MyService(rpyc.Service): def on_connect(self): """Think o+ this as a construct
import rpyc
test = ['hi']
myReferences= set()
class MyService(rpyc.Service):
def on_connect(self):
"""Think o+ this as a constructor of the class, but with
a new name so not to 'overload' the parent's init"""
self.fn = None
def exposed_setCallback(self,fn):
# i = 0
self.fn = fn # Saves the remote function for calling later
print self.fn
myReferences.add(self.fn)
#abc.append(i)
#i+=1
print myReferences
for x in myReferences:
print x
#print abc
if __name__ == "__main__":
# lists are pass by reference, so the same 'test'
# will be available to all threads
# While not required, think about locking!
from rpyc.utils.server import ThreadedServer
t = ThreadedServer(MyService, port = 18888)
t.start()
Answer:
<function myprint at 0x01FFD370>
set([<function myprint at 0x01FFD370>])
<function myprint at 0x01FFD370>
<function myprint at 0x022DD370>
set([<function myprint at 0x022DD370>,
导入rpyc
测试=['hi']
myReferences=set()
类MyService(rpyc.Service):
def接通(自):
“”“将o+视为类的构造函数,但使用
一个新名称,以避免“重载”父项的init“”
self.fn=无
def暴露_设置回调(自身,fn):
#i=0
self.fn=fn#保存远程函数以备以后调用
打印self.fn
myReferences.add(self.fn)
#abc.附加(一)
#i+=1
打印我的参考资料
对于myReferences中的x:
打印x
#印刷abc
如果名称=“\uuuuu main\uuuuuuuu”:
#列表是通过引用传递的,因此相同的“测试”
#将对所有线程可用
#虽然不是必需的,但请考虑锁定!
从rpyc.utils.server导入ThreadedServer
t=ThreadedServer(MyService,端口=18888)
t、 开始()
答复:
集合([])
集合([,,
请提供帮助如果要修改全局变量,应在函数顶部使用
global
语句
def exposed_setCallback(self, fn):
global myReferences
# i = 0
self.fn = fn # Saves the remote function for calling later
print self.fn
myReferences.add(self.fn)
我认为问题是因为您有一个
ThreadedServer
,它当然是多线程的
但是,(不允许多个线程同时访问它们),因此无论何时访问集合,都需要实现for,它处理为您获取/释放锁以及锁本身,一次只能由一个上下文管理器获取,因此无法同时访问您的集合。请参阅下面修改的代码:
import rpyc
import threading
test = ['hi']
myReferences= set()
myReferencesLock = threading.Lock()
class MyService(rpyc.Service):
def on_connect(self):
"""Think o+ this as a constructor of the class, but with
a new name so not to 'overload' the parent's init"""
self.fn = None
def exposed_setCallback(self,fn):
# i = 0
self.fn = fn # Saves the remote function for calling later
print self.fn
with myReferencesLock:
myReferences.add(self.fn)
#abc.append(i)
#i+=1
with myReferencesLock:
print myReferences
for x in myReferences:
print x
#print abc
if __name__ == "__main__":
# lists are pass by reference, so the same 'test'
# will be available to all threads
# While not required, think about locking!
from rpyc.utils.server import ThreadedServer
t = ThreadedServer(MyService, port = 18888)
t.start()
欢迎来到线程编程世界。请确保使用锁保护线程之间共享的数据!您的“答案”的最后一行不完整。我假设这只是一个复制/粘贴错误。请修复:)@三个菠萝:这就是问题,伙计。问题是它中途停止打印?是的,它没有正确插入元素。这与它无关。集合是可变的。我也尝试过。它仍然提供相同的输出。请suggest@Hyperboreus..你能建议一下如何纠正这个问题吗?我试过用这个..它不起作用..c你共享你的电子邮件地址。我可以向你转发我的2个python文件,这样你就可以有一个更清晰的图片。谢谢你的帮助!非常感谢。用它上传你文件的内容,编辑你的主要帖子以包含链接。这样每个人都可以看到和帮助。而且,即使这没有解决你的问题,你的问题仍然存在应该修复的代码(可能会给您带来问题)完成了..我还尝试使用简单的锁和Rlock..这两个问题都不起作用…请建议..谢谢!