How to do callbacks in rpyc 3.0

500 views
Skip to first unread message

fredri...@gmail.com

unread,
Aug 12, 2008, 6:35:44 PM8/12/08
to rpyc
Hello!

I have bin experimenting with rpyc and I must say I am very impressed.
However I can't get the callback to work properly. Basically what I'm
trying to do is to get the remote computer to call the callback
function once every second. This is my script:

---------------------------------------------------------
server.py:

import rpyc
import rpyc.utils.server
import time
import threading

class TestService(rpyc.Service):
def exposed_startTimer(self, callback):
callback = rpyc.async(callback)
self.timer = threading.Timer(1, self.doCallback, [callback,])
self.timer.start()

def exposed_stopTimer(self):
self.timer.cancel()

def doCallback(self, callback):
callback(time.time())
self.timer = threading.Timer(1, self.doCallback, [callback,])
self.timer.start()

server = rpyc.utils.server.ThreadedServer(TestService, port = 1234)
server.start()

---------------------------------------------------------
client.py

import rpyc
import time

def printTime(time):
print 'The remote time is ' + str(time)

connection = rpyc.connect('localhost', 1234)

connection.root.startTimer(printTime)
time.sleep(5)
connection.root.stopTimer()

---------------------------------------------------------

This works, except that all callbacks arrives first when I call
stopTimer(). I have also tried to start a new thread in the client
that calls connection.serve_all(). That works as long as the timer is
started, but then the call to stopTimer() never returns :/

I'm thankful for any help!
/Fredrik

tomer filiba

unread,
Aug 13, 2008, 6:23:03 AM8/13/08
to rp...@googlegroups.com
hi

connection = rpyc.connect('localhost', 1234)

connection.root.startTimer(printTime)
time.sleep(5)

well, if you want this to happen in the background you would indeed need to start
a thread to process incoming requests while your main thread sleeps.

def bg_server(conn):
    try:
        while True:
            conn.poll()
            time.sleep(0.1) # **see note**
    except EOFError:
        pass

t = threading.Thread(target = bg_server, args = (connection,))
t.start()

this would solve your problem.

note: this is a bug. i'm investigating this, but for some reason poll() causes the main thread to block.


hope it helps,
-tomer


--
An NCO and a Gentleman

fredri...@gmail.com

unread,
Aug 14, 2008, 7:09:50 AM8/14/08
to rpyc
> def bg_server(conn):
>     try:
>         while True:
>             conn.poll()
>             time.sleep(0.1) # **see note**
>     except EOFError:
>         pass
>
> t = threading.Thread(target = bg_server, args = (connection,))
> t.start()

That worked perfectly. Thanks!

fredri...@gmail.com

unread,
Aug 23, 2008, 9:08:54 AM8/23/08
to rpyc
Hello again :)

I have one more question regarding this. If you in the server skip the
line "callback = rpyc.async(callback)" the request will arrive at the
client, but the "callback(time.time())" will continue to block until
the client does another request (for example stopTimer()).

Is the response not sent by the client or is it not received by the
server, and is there a reason for that?

If there is anything I can do to help you in the development of this
excellent library, just tell me :)
/Fredrik
Reply all
Reply to author
Forward
0 new messages