Page 1 of 1

Pyro - remote connection and execute command

Posted: Mon Aug 21, 2017 10:47 pm
by VAntico
I'm looking for an example of remote connection to ShapCap script with Pyro.

My application is a client .net, I thought I was using Pyrolite.

Thanks,
Vito

Re: Pyro - remote connection and execute command

Posted: Tue Aug 22, 2017 3:54 pm
by admin
Hi Vito,

in SharpCap scripting, enter the sample Pyro4 server code:

Code: Select all

import Pyro4

@Pyro4.expose
class GreetingMaker(object):
    def get_fortune(self, name):
        return "Hello, {0}. Here is your fortune message:\n" \
               "Behold the warranty -- the bold print giveth and the fine print taketh away.".format(name)

daemon = Pyro4.Daemon()                # make a Pyro daemon
uri = daemon.register(GreetingMaker)   # register the greeting maker as a Pyro object

print("Ready. Object uri =", uri)      # print the uri so we can use it in the client later
daemon.requestLoop()                   # start the event loop of the server to wait for calls
This should show a URI to connect to - it will change every time - something like

Code: Select all

PYRO:obj_871d6f34a2fa4e5aa56a60b1655cd270@localhost:32081
Now in another python environment, enter

Code: Select all

import Pyro4
greeting_maker = Pyro4.Proxy('PYRO:obj_871d6f34a2fa4e5aa56a60b1655cd270@localhost:32081')
print(greeting_maker.get_fortune('robin'))
Obviously change the URI to the one produced by your SharpCap scripting.

I haven't tried connecting from a library like Pyrolite, but I'd suggest getting the above working correctly then attempting to connect from Pyrolite.

cheers,

Robin

Re: Pyro - remote connection and execute command

Posted: Tue Aug 22, 2017 3:58 pm
by admin
Just as a note, you cannot 'expose' the SharpCap scripting objects like the SharpCap application, Cameras, Controls, etc since they are strange proxies to .NET objects and incompatible with pyro. You will need to write plain python classes to make the functionality you want available and expose those.

cheers,

Robin

Re: Pyro - remote connection and execute command

Posted: Tue Aug 22, 2017 4:40 pm
by oopfan
Very neat!

Re: Pyro - remote connection and execute command

Posted: Tue Aug 22, 2017 9:07 pm
by VAntico
Hi Robin.
Thanks for the quick support.
I was referring to the same code segment, thinking about expanding the SharpCap methods I needed ;) .

Code: Select all

import Pyro4
@Pyro4.expose

class ext_SharpCap(object):
    def get_test(self, name):
        return "Test, {0}.".format(name)
        
daemon = Pyro4.Daemon()                        # make a Pyro daemon
 #ns = Pyro4.locateNS()                             # find the name server
uri = daemon.register(ext_SharpCap)        # register the SharpCap as a Pyro object 
  #ns.register("ext.SharpCap", uri)             # register the object with a name in the name server
print("Ready. Object uri =", uri)                 # print the uri so we can use it in the client later
daemon.requestLoop()                              # Problem: Before not possible STOP or Exit from program.
I found two needs:
  • After you run the script line
    daemon.requestLoop()
    It is no longer possible to stop executing scripts, nor even close the program. You have to kill the application from system manager.
  • Every time the script restarts, the Pyro URI changes. The solution, maybe, is to use the "Pyro name server", but needs a Python context-driven application with a separate command: "python -m Pyro4.naming", as described in: http://pythonhosted.org//Pyro4/intro.ht ... ame-server.
Thanks
Vito

Re: Pyro - remote connection and execute command

Posted: Tue Aug 22, 2017 9:27 pm
by admin
I think you can fix the lockup problem just by running the requestLoop in a separate thread - ie

Code: Select all

import Pyro4
import thread

@Pyro4.expose
class GreetingMaker(object):
    def get_fortune(self, name):
        return "Hello, {0}. Here is your fortune message:\n" \
               "Behold the warranty -- the bold print giveth and the fine print taketh away.".format(name)

daemon = Pyro4.Daemon()                # make a Pyro daemon
uri = daemon.register(GreetingMaker)   # register the greeting maker as a Pyro object

print("Ready. Object uri =", uri)      # print the uri so we can use it in the client later

thread.start_new_thread(daemon.requestLoop, ())
The scripting runs in its own thread anyway (not the main application thread), so this shouldn't add any new cross thread problems.

cheers,

Robin

Re: Pyro - remote connection and execute command

Posted: Wed Aug 23, 2017 3:45 pm
by VAntico
OK thanks!
Scripts now work, and lets you exit from the program.
The URI connection works.

Obviously, every time you restart the script/program, the URI changes :)

Cheers,
Vito

Re: Pyro - remote connection and execute command

Posted: Wed Aug 23, 2017 4:13 pm
by admin
You could always make the python open up a known TCP port for incoming requests and make that port give out the Pyro URI to the calling application, then the calling application would connect to port 9999 (or whatever), get the Pyro URI and then connect properly.

cheers,

Robin