|
20 | 20 | from weakref import \ |
21 | 21 | ref as weak_ref, \ |
22 | 22 | WeakValueDictionary |
| 23 | +import threading |
23 | 24 | import io |
24 | 25 | import atexit |
25 | 26 | import asyncio |
@@ -1937,6 +1938,43 @@ def open(celf, address, private, error = None) : |
1937 | 1938 | result |
1938 | 1939 | #end open |
1939 | 1940 |
|
| 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 | + |
1940 | 1978 | def close(self) : |
1941 | 1979 | dbus.dbus_connection_close(self._dbobj) |
1942 | 1980 | #end close |
|
0 commit comments