Skip to content

Commit 9813952

Browse files
author
Lawrence D'Oliveiro
committed
add dbussy.Connection.open_async
1 parent 63485b4 commit 9813952

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

dbussy.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from weakref import \
2121
ref as weak_ref, \
2222
WeakValueDictionary
23+
import threading
2324
import io
2425
import atexit
2526
import asyncio
@@ -1937,6 +1938,43 @@ def open(celf, address, private, error = None) :
19371938
result
19381939
#end open
19391940

1941+
@classmethod
1942+
async def open_async(celf, address, private, error = None, loop = None) :
1943+
"opens a Connection to a specified address, separate from the" \
1944+
" system or session buses."
1945+
# There is no nonblocking version of dbus_connection_open/dbus_connection_open_private,
1946+
# so I invoke it in a separate thread.
1947+
1948+
if loop == None :
1949+
loop = asyncio.get_event_loop()
1950+
#end if
1951+
error, my_error = _get_error(error)
1952+
awaiting = loop.create_future()
1953+
1954+
async def do_open_done(result) :
1955+
awaiting.set_result(result)
1956+
#end def do_open_done
1957+
1958+
def do_open() :
1959+
result = (dbus.dbus_connection_open, dbus.dbus_connection_open_private)[private](address.encode(), error._dbobj)
1960+
# A Future is not itself threadsafe, but I can thread-safely
1961+
# create a coroutine on the main thread to set it.
1962+
asyncio.run_coroutine_threadsafe(do_open_done(result), loop)
1963+
#end do_open
1964+
1965+
#begin open_async
1966+
subthread = threading.Thread(target = do_open)
1967+
subthread.start()
1968+
result = await awaiting
1969+
my_error.raise_if_set()
1970+
if result != None :
1971+
result = celf(result)
1972+
result.attach_asyncio(loop)
1973+
#end if
1974+
return \
1975+
result
1976+
#end open_async
1977+
19401978
def close(self) :
19411979
dbus.dbus_connection_close(self._dbobj)
19421980
#end close

0 commit comments

Comments
 (0)