Blame SOURCES/0002-Support-asynchronous-calls-58.patch

a2f718
From 31d6dd7893a5e1bb9eb14bfcee861a5b62f64960 Mon Sep 17 00:00:00 2001
a2f718
From: Vendula Poncova <vponcova@redhat.com>
a2f718
Date: Thu, 27 Jul 2017 18:41:29 +0200
a2f718
Subject: [PATCH 2/3] Support asynchronous calls (#58)
a2f718
a2f718
Added support for asynchronous calls of methods. A method is called
a2f718
synchronously unless its callback parameter is specified. A callback
a2f718
is a function f(*args, returned=None, error=None), where args is
a2f718
callback_args specified in the method call, returned is a return
a2f718
value of the method and error is an exception raised by the method.
a2f718
a2f718
Example of an asynchronous call:
a2f718
a2f718
def func(x, y, returned=None, error=None):
a2f718
  pass
a2f718
a2f718
proxy.Method(a, b, callback=func, callback_args=(x, y))
a2f718
---
a2f718
 doc/tutorial.rst       | 11 ++++++++-
a2f718
 pydbus/proxy_method.py | 44 ++++++++++++++++++++++++++++++-----
a2f718
 tests/publish_async.py | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++
a2f718
 tests/run.sh           |  1 +
a2f718
 4 files changed, 112 insertions(+), 7 deletions(-)
a2f718
 create mode 100644 tests/publish_async.py
a2f718
a2f718
diff --git a/doc/tutorial.rst b/doc/tutorial.rst
a2f718
index 7474de3..b8479cf 100644
a2f718
--- a/doc/tutorial.rst
a2f718
+++ b/doc/tutorial.rst
a2f718
@@ -84,7 +84,8 @@ All objects have methods, properties and signals.
a2f718
 Setting up an event loop
a2f718
 ========================
a2f718
 
a2f718
-To handle signals emitted by exported objects, or to export your own objects, you need to setup an event loop.
a2f718
+To handle signals emitted by exported objects, to asynchronously call methods
a2f718
+or to export your own objects, you need to setup an event loop.
a2f718
 
a2f718
 The only main loop supported by ``pydbus`` is GLib.MainLoop.
a2f718
 
a2f718
@@ -156,6 +157,14 @@ To call a method::
a2f718
 
a2f718
     dev.Disconnect()
a2f718
 
a2f718
+To asynchronously call a method::
a2f718
+
a2f718
+    def print_result(returned=None, error=None):
a2f718
+        print(returned, error)
a2f718
+
a2f718
+    dev.GetAppliedConnection(0, callback=print_result)
a2f718
+    loop.run()
a2f718
+
a2f718
 To read a property::
a2f718
 
a2f718
     print(dev.Autoconnect)
a2f718
diff --git a/pydbus/proxy_method.py b/pydbus/proxy_method.py
a2f718
index 3e6e6ee..442fe07 100644
a2f718
--- a/pydbus/proxy_method.py
a2f718
+++ b/pydbus/proxy_method.py
a2f718
@@ -65,15 +65,34 @@ class ProxyMethod(object):
a2f718
 
a2f718
 		# Python 2 sux
a2f718
 		for kwarg in kwargs:
a2f718
-			if kwarg not in ("timeout",):
a2f718
+			if kwarg not in ("timeout", "callback", "callback_args"):
a2f718
 				raise TypeError(self.__qualname__ + " got an unexpected keyword argument '{}'".format(kwarg))
a2f718
 		timeout = kwargs.get("timeout", None)
a2f718
+		callback = kwargs.get("callback", None)
a2f718
+		callback_args = kwargs.get("callback_args", tuple())
a2f718
+
a2f718
+		call_args = (
a2f718
+			instance._bus_name,
a2f718
+			instance._path,
a2f718
+			self._iface_name,
a2f718
+			self.__name__,
a2f718
+			GLib.Variant(self._sinargs, args),
a2f718
+			GLib.VariantType.new(self._soutargs),
a2f718
+			0,
a2f718
+			timeout_to_glib(timeout),
a2f718
+			None
a2f718
+		)
a2f718
+
a2f718
+		if callback:
a2f718
+			call_args += (self._finish_async_call, (callback, callback_args))
a2f718
+			instance._bus.con.call(*call_args)
a2f718
+			return None
a2f718
+		else:
a2f718
+			ret = instance._bus.con.call_sync(*call_args)
a2f718
+			return self._unpack_return(ret)
a2f718
 
a2f718
-		ret = instance._bus.con.call_sync(
a2f718
-			instance._bus_name, instance._path,
a2f718
-			self._iface_name, self.__name__, GLib.Variant(self._sinargs, args), GLib.VariantType.new(self._soutargs),
a2f718
-			0, timeout_to_glib(timeout), None).unpack()
a2f718
-
a2f718
+	def _unpack_return(self, values):
a2f718
+		ret = values.unpack()
a2f718
 		if len(self._outargs) == 0:
a2f718
 			return None
a2f718
 		elif len(self._outargs) == 1:
a2f718
@@ -81,6 +100,19 @@ class ProxyMethod(object):
a2f718
 		else:
a2f718
 			return ret
a2f718
 
a2f718
+	def _finish_async_call(self, source, result, user_data):
a2f718
+		error = None
a2f718
+		return_args = None
a2f718
+
a2f718
+		try:
a2f718
+			ret = source.call_finish(result)
a2f718
+			return_args = self._unpack_return(ret)
a2f718
+		except Exception as err:
a2f718
+			error = err
a2f718
+
a2f718
+		callback, callback_args = user_data
a2f718
+		callback(*callback_args, returned=return_args, error=error)
a2f718
+
a2f718
 	def __get__(self, instance, owner):
a2f718
 		if instance is None:
a2f718
 			return self
a2f718
diff --git a/tests/publish_async.py b/tests/publish_async.py
a2f718
new file mode 100644
a2f718
index 0000000..3f79b62
a2f718
--- /dev/null
a2f718
+++ b/tests/publish_async.py
a2f718
@@ -0,0 +1,63 @@
a2f718
+from pydbus import SessionBus
a2f718
+from gi.repository import GLib
a2f718
+from threading import Thread
a2f718
+import sys
a2f718
+
a2f718
+done = 0
a2f718
+loop = GLib.MainLoop()
a2f718
+
a2f718
+class TestObject(object):
a2f718
+	'''
a2f718
+<node>
a2f718
+	<interface name='net.lew21.pydbus.tests.publish_async'>
a2f718
+		<method name='HelloWorld'>
a2f718
+			<arg type='i' name='x' direction='in'/>
a2f718
+			<arg type='s' name='response' direction='out'/>
a2f718
+		</method>
a2f718
+	</interface>
a2f718
+</node>
a2f718
+	'''
a2f718
+	def __init__(self, id):
a2f718
+		self.id = id
a2f718
+
a2f718
+	def HelloWorld(self, x):
a2f718
+		res = self.id + ": " + str(x)
a2f718
+		print(res)
a2f718
+		return res
a2f718
+
a2f718
+bus = SessionBus()
a2f718
+
a2f718
+with bus.publish("net.lew21.pydbus.tests.publish_async", TestObject("Obj")):
a2f718
+	remote = bus.get("net.lew21.pydbus.tests.publish_async")
a2f718
+
a2f718
+	def callback(x, returned=None, error=None):
a2f718
+		print("asyn: " + returned)
a2f718
+		assert (returned is not None)
a2f718
+		assert(error is None)
a2f718
+		assert(x == int(returned.split()[1]))
a2f718
+
a2f718
+		global done
a2f718
+		done += 1
a2f718
+		if done == 3:
a2f718
+			loop.quit()
a2f718
+
a2f718
+	def t1_func():
a2f718
+		remote.HelloWorld(1, callback=callback, callback_args=(1,))
a2f718
+		remote.HelloWorld(2, callback=callback, callback_args=(2,))
a2f718
+		print("sync: " + remote.HelloWorld(3))
a2f718
+		remote.HelloWorld(4, callback=callback, callback_args=(4,))
a2f718
+
a2f718
+	t1 = Thread(None, t1_func)
a2f718
+	t1.daemon = True
a2f718
+
a2f718
+	def handle_timeout():
a2f718
+		print("ERROR: Timeout.")
a2f718
+		sys.exit(1)
a2f718
+
a2f718
+	GLib.timeout_add_seconds(2, handle_timeout)
a2f718
+
a2f718
+	t1.start()
a2f718
+
a2f718
+	loop.run()
a2f718
+
a2f718
+	t1.join()
a2f718
diff --git a/tests/run.sh b/tests/run.sh
a2f718
index 8d93644..271c58a 100755
a2f718
--- a/tests/run.sh
a2f718
+++ b/tests/run.sh
a2f718
@@ -15,4 +15,5 @@ then
a2f718
 	"$PYTHON" $TESTS_DIR/publish.py
a2f718
 	"$PYTHON" $TESTS_DIR/publish_properties.py
a2f718
 	"$PYTHON" $TESTS_DIR/publish_multiface.py
a2f718
+	"$PYTHON" $TESTS_DIR/publish_async.py
a2f718
 fi
a2f718
-- 
a2f718
2.13.5
a2f718