Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pyro4不允许两个以上的客户端访问一个URI_Python_Concurrency_Network Programming_Pyro - Fatal编程技术网

Python Pyro4不允许两个以上的客户端访问一个URI

Python Pyro4不允许两个以上的客户端访问一个URI,python,concurrency,network-programming,pyro,Python,Concurrency,Network Programming,Pyro,我正在使用pygame在Python中创建一个基于回合的策略游戏。我发现编写套接字非常困难,所以我求助于Pyro来分享游戏板的状态。但是,Pyro似乎无法同时支持2个以上的连接 我正在本地主机上通过运行名称服务器 python -m Pyro4.naming 测试用例“服务器”: import Pyro4 class Testcase: def __init__(self): self.values = [1, 2, 3, 10, 20, 30] def as

我正在使用pygame在Python中创建一个基于回合的策略游戏。我发现编写套接字非常困难,所以我求助于Pyro来分享游戏板的状态。但是,Pyro似乎无法同时支持2个以上的连接

我正在本地主机上通过运行名称服务器

python -m Pyro4.naming
测试用例“服务器”:

import Pyro4
class Testcase:
    def __init__(self):
        self.values = [1, 2, 3, 10, 20, 30]

    def askvalue(self, i):
        return self.values[i]


daemon = Pyro4.Daemon()
ns = Pyro4.locateNS()

uri = daemon.register(Testcase())
ns.register("thetest", uri)
daemon.requestLoop()
客户:

import Pyro4, time

ns = Pyro4.locateNS()

casetester = Pyro4.Proxy("PYRONAME:thetest")

while True:
    print "Accessing remote object:"
    print casetester.askvalue(1)
    print "staying busy"
    time.sleep(10)
前两个客户端的输出:

/usr/local/lib/python2.7/dist-packages/Pyro4-4.14-py2.7.egg/Pyro4/core.py:155: UserWarning: HMAC_KEY not set, protocol data may not be secure
  warnings.warn("HMAC_KEY not set, protocol data may not be secure")
Accessing remote object:
2
staying busy
Accessing remote object:
2
staying busy
重复

来自第三个客户端的输出:

/usr/local/lib/python2.7/dist-packages/Pyro4-4.14-py2.7.egg/Pyro4/core.py:155: UserWarning: HMAC_KEY not set, protocol data may not be secure
  warnings.warn("HMAC_KEY not set, protocol data may not be secure")
Accessing remote object:
然后挂起来

第四个、第五个(可能是所有其他)客户端的输出:

在这个阶段,我给命名服务器a^C和客户端3,4。。。给出此输出并崩溃:

Traceback (most recent call last):
  File "client.py", line 3, in <module>
    ns = Pyro4.locateNS()
  File "/usr/local/lib/python2.7/dist-packages/Pyro4-4.14-py2.7.egg/Pyro4/naming.py", line 323, in locateNS
    raise Pyro4.errors.NamingError("Failed to locate the nameserver")
Pyro4.errors.NamingError: Failed to locate the nameserver
回溯(最近一次呼叫最后一次):
文件“client.py”,第3行,在
ns=Pyro4.locateNS()
文件“/usr/local/lib/python2.7/dist packages/Pyro4-4.14-py2.7.egg/Pyro4/naming.py”,第323行,位置为
raise Pyro4.errors.NamingError(“未能找到名称服务器”)
Pyro4.errors.NamingError:未能找到名称服务器
同时,客户端1和客户端2保持忙碌

然而,破坏其中一个活动客户端会让其中一个挂起的客户端开始运行

我曾尝试通过“export PYRO_SERVERTYPE=multiplex”切换线程,但这并没有改变行为。最大连接的设置似乎是200。将其设置为1000也不能解决我的问题

我已经读到Pyro缺乏可扩展性,但我肯定能够获得至少10个连接


如何一次将两个以上的客户端连接到Pyro4对象

回答我自己的问题,希望这会很快出现在谷歌上

这不是一个“完美”的答案,但却是一个可行的难题。服务器代码保持不变,但客户端代码变为:

import Pyro4, time


global ns
global casetester

def connect():
    global ns
    global casetester
    ns = Pyro4.locateNS()
    casetester = Pyro4.Proxy("PYRONAME:thetest")

def disconnect():
    global ns
    global casetester
    del ns
    del casetester


while True:
    print "Accessing remote object:"
    connect()
    print casetester.askvalue(1)
    disconnect()
    print "staying busy"
    time.sleep(3)
到处都是额外的“全局”,因为永远不要假设

为什么这样做有效?因为我建立了连接,访问远程对象,然后删除连接


我觉得这个解决方案很难看,但我会一直使用它,直到找到“正确”的方法。

我也遇到过类似的问题。我独立地找到了您的
全局
解决方案,但很长一段时间都没有用,随着应用程序规模的增加,错误又出现了。经过多次尝试和错误,我想我现在可能已经发现了问题…用户错误

根据,名称服务器本身是一个代理。因此,我确保像我的其他pyro代理一样,将它们与“with”语句一起使用


同样地,在您的
ns.register('test',uri)
调用中对名称服务器的每一次引用都做同样的事情。我有完全相同的考虑。但是当我与您运行类似的代码时,我发现Pyro4(2014-12)没有这样的问题

我测试了10个客户端同时调用它。 它工作得很好

我写这篇文章是为了对像我这样的人有用

服务器代码 客户
谢谢你的想法,我一定会尝试的。
import Pyro4, time


global ns
global casetester

def connect():
    global ns
    global casetester
    ns = Pyro4.locateNS()
    casetester = Pyro4.Proxy("PYRONAME:thetest")

def disconnect():
    global ns
    global casetester
    del ns
    del casetester


while True:
    print "Accessing remote object:"
    connect()
    print casetester.askvalue(1)
    disconnect()
    print "staying busy"
    time.sleep(3)
with Pyro4.locateNS() as ns:
  uri = ns.lookup('Object name')
with Pyro4.proxy(uri) as obj:
  obj.SomeMethod()
import Pyro4
import time
class Testcase:
    def __init__(self):
        self.value = 1

    def askvalue(self, i):
        # Simulate doing some work.
        time.sleep(1)

        self.value += 1
        return self.value


daemon = Pyro4.Daemon()
ns = Pyro4.locateNS()

uri = daemon.register(Testcase())
ns.register("thetest", uri)
daemon.requestLoop()
import Pyro4, time

ns = Pyro4.locateNS()

casetester = Pyro4.Proxy("PYRONAME:thetest")

while True:
    print("Accessing remote object:")
    print (casetester.askvalue(1))
    print ("staying busy")