简体   繁体   English

Python Pyro4远程对象块(如已死锁)

[英]Python Pyro4 remote object block (like if it was deadlocked)

I have a problem with Python Pyro4 remote objects which behave as if they were deadlock. 我的Python Pyro4远程对象有问题,就好像它们死锁了一样。 Here's how to reproduce the problem (in Windows). 这是重现此问题的方法(在Windows中)。 Start the name server: 启动名称服务器:

set PYRO_HMAC_KEY=some_key
python -m Pyro4.naming

Then run the remote object server: 然后运行远程对象服务器:

import Pyro4
import sys


class Scheduler:
    def test(self):
        pass

if __name__ == '__main__': 

    sys.excepthook = Pyro4.util.excepthook

    scheduler = Scheduler()

    Pyro4.config.HMAC_KEY='some_key'  

    deamon = Pyro4.Daemon()
    ns = Pyro4.locateNS()
    ns.register("scheduler", deamon.register(scheduler))

    deamon.requestLoop()

Then run the client: 然后运行客户端:

import sys
from multiprocessing import Process
import Pyro4

class BWModule(Process):

    def __init__(self):
        Process.__init__(self)
        self.depth = 1


    def run(self): 

        Pyro4.config.HMAC_KEY='some_key'       
        self.scheduler = Pyro4.Proxy("PYRONAME:scheduler")                

        print "1"
        sys.stdout.flush()         
        self.scheduler.test()
        print "2"
        sys.stdout.flush()         

        print "depth", self.depth
        sys.stdout.flush() 

        if self.depth < 5:
            for i in range(10):
                newblock = self.duplicate()
                newblock.depth = self.depth + 1
                newblock.start()


    def duplicate(self):
        dup = type(self)()
        return dup



if __name__ == '__main__': 

    sys.excepthook = Pyro4.util.excepthook

    No1 = BWModule()    
    No1.start()

When I run this code, I see printed message "depth X" with X from 1 to 3 but no more. 运行此代码时,我看到打印的消息“深度X”,其中X从1到3,但没有更多。 I also see at the end of the execution (before nothing else happen) a bunch of 1s without corresponding 2s, which indicates that the call the the Pyro remote object "self.scheduler.test()" has blocked. 我还看到在执行结束时(之前什么也没有发生),一串1没有对应的2,这表示Pyro远程对象“ self.scheduler.test()”的调用已被阻止。 This makes me think that this is a problem with Pyro rather than with processes (for example, running out of available processes if such a thing is possible). 这使我认为这是Pyro的问题,而不是进程的问题(例如,如果可能的话,用尽可用的进程)。 However, if I reduce the process multiplication from 10 to 2 (ie, replacing "for i in range(10):" by "for i in range(2):" in the client code), the execution goes all the way to depth 5 without blocking... 但是,如果我将进程乘法从10减少到2(即在客户端代码中将“ for range(10):i替换为for for range(2):i”),则执行将一直执行到深度5无阻塞...

So here are my questions: What is happining? 所以这是我的问题:什么是令人开心的? Why does it block with "for i in range(10):"? 为什么用“ for i in range(10):”阻止? Is there a limit of possible "client" processes to a Pyro4 remote object or something like that? Pyro4远程对象或类似对象的可能“客户端”进程是否受到限制? Is it a deadlocking problem? 这是一个僵局的问题吗?

Thank you. 谢谢。

Ok. 好。 For anyone facing a similar problem, this problem seems to be associated with some limit to the number of Pyro proxies that can hold simultaneously a reference on a remote object. 对于面临类似问题的任何人,此问题似乎与可以同时保存远程对象上的引用的Pyro代理数量的限制有关。 Beware however that I'm only conjecturing this. 但是请注意,我只是在猜测。 This conclusion is based on the fact that adding 该结论基于以下事实:

del self.scheduler

right before 就在之前

if self.depth < 5:

in the client code both limits the number of alive proxies and fixes the problem (giving it enough time, the code go up to depth 5 using "for i in range(10):"). 在客户端代码中,这既限制了代理的数量,又解决了该问题(给予足够的时间,代码使用“ for range in 10(10 :)”上升到深度5)。

EDIT: 编辑:

As I understand it from here and from some testing, the value of THREADPOOL_MINTHREADS is linked to the maximal number of connections or proxies to a remote object. 据我从这里和一些测试中了解到的,THREADPOOL_MINTHREADS的值链接到远程对象的最大连接数或代理数。 If I set it to 4, the program freeze (much like if it was deadlock) when the fifth proxy tries to acces my remote object. 如果将其设置为4,则当第五个代理尝试访问我的远程对象时,程序将冻结(就像死锁一样)。

There is also some possibly related information here . 也有一些可能相关的信息在这里

Just faced a similar issue, a neater way to do this might be to change 刚遇到类似的问题,更巧妙的方法可能是改变

 self.scheduler = Pyro4.Proxy("PYRONAME:scheduler") 

to

 with Pyro4.Proxy("PYRONAME:scheduler") as self.scheduler:
    # Rest of code using self.scheduler
 #Rest of code not using self.scheduler

This is equivalent to using del self.scheduler except you don't need to worry about where you place this - python does the heavy lifting for you. 这等效于使用del self.scheduler不同之处在于,您无需担心将其放置在什么地方del self.scheduler为您完成了繁重的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM