irmen / Pyro5

Pyro 5 - Python remote objects
https://pyro5.readthedocs.io
MIT License
303 stars 36 forks source link

Multi-Threading a Remote Object Method #69

Closed JonesBareBones closed 1 year ago

JonesBareBones commented 1 year ago

This isn't so much an issue with the very nice and well documented package that is Pyro, rather with my own knowledge in python and multi-threading. I'd like to call methods in my remote object multiple times without waiting for each to finish in sequence (my understanding of multi-threading), like so:

In client class on my local machine

    for i in range(1,2):
        _thread.start_new_thread(self.proxyServer.sleepyFunction.(args))

On the remote object

def sleepyFunction(self):
    import time
    time.sleep(5)
    print('server wakes up')

This doesn't work as the remote object complains about not belonging to this new thread I'm making. You mention in the documentation that I can set the thread pools on the name-server, and that I can change the ownership of a thread, but my intellect doesn't allow me to tie it all together, could you perhaps explain a bit more how one could achieve multi-threading on the remote object?

I could instantiate the threads on the remote object, and get around it, but the threads are intended to depend on circumstances that arise on the client side. It's difficult to explain, but I could for example want to run method A and method B on the server on demand from the client.

irmen commented 1 year ago

Ok basically you need to create a separate proxy object per thread in your client. See https://pyro5.readthedocs.io/en/latest/clientcode.html#proxy-sharing-between-threads
The easiest pattern probably is to defer creating the actual proxy in the thread that is going to use it, rather than creating a proxy somewhere else and handing that to the thread. Instead maybe only hand the Pyro URI to the thread, that is safe. With multiple proxy objects making calls, Pyro's default server type will automatically create a new thread in the server for every connection (=every proxy object) - you don't have to do anything for that.

However, you may have to decide/change how your Pyro server is going to deal with the concurrent calls. This is how you want to share your data (or not). See https://pyro5.readthedocs.io/en/latest/servercode.html#controlling-instance-modes-and-instance-creation

Finally if you dig around in Pyro5 examples, you will find various examples that touch on this subject.