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

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