Question

I'm new to python and have been struggling with this for quite a while. Okay so I have a program that logs into a server and then constantly pings the server every 10 seconds to see if the status of the server has changed. In the same script I have a function which sends a message to the server.

a simple example of the send method:

def send(self, message):

   url = ("https://testserver.com/socket?message=%s") % (message)

   req = urllib2.Request(url, None, None)
   response = urllib2.urlopen(req).read()

   print response

would it be possible for me to call this method from another script while this one was running? as in using the same session. It seems as though when I run my script which calls this function it creates a new instance of it instead of using the current instance of that script causing it to throw my exception saying I am not connected to the server.

Sorry for the noob question. I have tried googling for a while but I cant seem to find the answer. I have read the following but these didn't solve the problem:

Python call function within class

Python code to get current function into a variable?

Hi @nFreeze thanks for the reply I have tried to use ZeroRPC but every time I run the script/example you gave (obviously edited) I run into this error:

Traceback (most recent call last):
    File "C:\Users\dwake\Desktop\Web Projects\test.py", line 1, in <module>
import zerorpc
    File "C:\Python27\lib\site-packages\zerorpc\__init__.py", line 27, in <module>
from .context import *
    File "C:\Python27\lib\site-packages\zerorpc\context.py", line 29, in <module>
import gevent_zmq as zmq
    File "C:\Python27\lib\site-packages\zerorpc\gevent_zmq.py", line 33, in <module>
import gevent.event
    File "C:\Python27\lib\site-packages\gevent\__init__.py", line 48, in <module>
from gevent.greenlet import Greenlet, joinall, killall
    File "C:\Python27\lib\site-packages\gevent\greenlet.py", line 6, in <module>
from gevent.hub import greenlet, getcurrent, get_hub, GreenletExit, Waiter
    File "C:\Python27\lib\site-packages\gevent\hub.py", line 30, in <module>
greenlet = __import__('greenlet').greenlet
ImportError: No module named greenlet

Even though I have installed gevent. I'm not sure how to fix this. Have been googling for a good hour now.

Was it helpful?

Solution

What you're looking for is called an RPC server. It allows external clients to execute exposed functions in your app. Luckily python has many RPC options. ZeroRPC is probably my favorite as it is easy to use and supports node.js. Here is an example of how to expose your send method using ZeroRPC:

In your app (server)

import zerorpc

class HelloRPC(object):
    def send(self, message):
        url = ("https://testserver.com/socket?message=%s") % (message)
        req = urllib2.Request(url, None, None)
        response = urllib2.urlopen(req).read()
        return response    
s = zerorpc.Server(HelloRPC())
s.bind("tcp://0.0.0.0:4242")
s.run()

In the other app (client)

import zerorpc

c = zerorpc.Client()
c.connect("tcp://127.0.0.1:4242")
print c.send("RPC TEST!")

OTHER TIPS

Simpliest way is to use UNIX signals. You'll need no third-party libraries.

# your-daemon.py
import signal
from time import sleep

def main():
    while True:
        print "Do some job..."
        sleep(5)

def send():
    print "Send your data"

def onusr1(*args):
    send()

if __name__ == '__main__':
    signal.signal(signal.SIGUSR1, onusr1)
    main()

Run in terminal:

$ pgrep -f your-daemon.py | xargs kill -SIGUSR1

Of course, this works only in local machine. Also you can't specify any argument to send function, and if you want to have many handlers, then use RPC as adviced below.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top