forked from LEW21/pydbus
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support asynchronous calls (LEW21#58)
Added support for asynchronous calls of methods. A method is called synchronously unless its callback parameter is specified. A callback is a function f(*args, returned=None, error=None), where args is callback_args specified in the method call, returned is a return value of the method and error is an exception raised by the method. Example of an asynchronous call: def func(x, y, returned=None, error=None): pass proxy.Method(a, b, callback=func, callback_args=(x, y))
- Loading branch information
Showing
3 changed files
with
106 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
from pydbus import SessionBus | ||
from gi.repository import GLib | ||
from threading import Thread | ||
import sys | ||
|
||
done = 0 | ||
loop = GLib.MainLoop() | ||
|
||
class TestObject(object): | ||
''' | ||
<node> | ||
<interface name='net.lew21.pydbus.tests.publish_async'> | ||
<method name='HelloWorld'> | ||
<arg type='i' name='x' direction='in'/> | ||
<arg type='s' name='response' direction='out'/> | ||
</method> | ||
<method name='Quit'/> | ||
</interface> | ||
</node> | ||
''' | ||
def __init__(self, id): | ||
self.id = id | ||
|
||
def HelloWorld(self, x): | ||
res = self.id + ": " + str(x) | ||
print(res) | ||
return res | ||
|
||
def Quit(self): | ||
loop.quit() | ||
|
||
bus = SessionBus() | ||
|
||
with bus.publish("net.lew21.pydbus.tests.publish_async", TestObject("Obj")): | ||
remote = bus.get("net.lew21.pydbus.tests.publish_async") | ||
|
||
def callback(x, returned=None, error=None): | ||
print("asyn: " + returned) | ||
assert (returned is not None) | ||
assert(error is None) | ||
assert(x == int(returned.split()[1])) | ||
|
||
global done | ||
done += 1 | ||
if done == 3: | ||
loop.quit() | ||
|
||
def t1_func(): | ||
remote.HelloWorld(1, callback=callback, callback_args=(1,)) | ||
remote.HelloWorld(2, callback=callback, callback_args=(2,)) | ||
print("sync: " + remote.HelloWorld(3)) | ||
remote.HelloWorld(4, callback=callback, callback_args=(4,)) | ||
|
||
t1 = Thread(None, t1_func) | ||
t1.daemon = True | ||
|
||
def handle_timeout(): | ||
print("ERROR: Timeout.") | ||
sys.exit(1) | ||
|
||
GLib.timeout_add_seconds(2, handle_timeout) | ||
|
||
t1.start() | ||
|
||
loop.run() | ||
|
||
t1.join() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters