diff --git a/aioupnp/__init__.py b/aioupnp/__init__.py index 335075f..a1d190c 100644 --- a/aioupnp/__init__.py +++ b/aioupnp/__init__.py @@ -4,9 +4,3 @@ __author__ = "Jack Robison" __maintainer__ = "Jack Robison" __license__ = "MIT" __email__ = "jackrobison@lbry.io" - -import sys -import asyncio - -if not hasattr(asyncio, "get_running_loop") and hasattr(asyncio, "_get_running_loop"): # python 3.6 - setattr(sys.modules['asyncio'], "get_running_loop", asyncio._get_running_loop) diff --git a/aioupnp/protocols/scpd.py b/aioupnp/protocols/scpd.py index 25ef079..30c26d2 100644 --- a/aioupnp/protocols/scpd.py +++ b/aioupnp/protocols/scpd.py @@ -92,7 +92,7 @@ class SCPDHTTPClientProtocol(Protocol): async def scpd_get(control_url: str, address: str, port: int) -> typing.Tuple[typing.Dict, bytes, typing.Optional[Exception]]: - loop = asyncio.get_running_loop() + loop = asyncio.get_event_loop_policy().get_event_loop() finished: asyncio.Future = asyncio.Future() packet = serialize_scpd_get(control_url, address) transport, protocol = await loop.create_connection( @@ -118,7 +118,7 @@ async def scpd_get(control_url: str, address: str, port: int) -> typing.Tuple[ty async def scpd_post(control_url: str, address: str, port: int, method: str, param_names: list, service_id: bytes, soap_socket: socket.socket = None, **kwargs) -> typing.Tuple[typing.Dict, bytes, typing.Optional[Exception]]: - loop = asyncio.get_running_loop() + loop = asyncio.get_event_loop_policy().get_event_loop() finished: asyncio.Future = asyncio.Future() packet = serialize_soap_post(method, param_names, service_id, address.encode(), control_url.encode(), **kwargs) transport, protocol = await loop.create_connection( diff --git a/aioupnp/protocols/ssdp.py b/aioupnp/protocols/ssdp.py index 064790e..5238c2c 100644 --- a/aioupnp/protocols/ssdp.py +++ b/aioupnp/protocols/ssdp.py @@ -111,7 +111,7 @@ class SSDPProtocol(MulticastProtocol): async def listen_ssdp(lan_address: str, gateway_address: str, ssdp_socket: socket.socket = None, ignored: typing.Set[str] = None, unicast: bool = False) -> typing.Tuple[DatagramTransport, SSDPProtocol, str, str]: - loop = asyncio.get_running_loop() + loop = asyncio.get_event_loop_policy().get_event_loop() try: sock = ssdp_socket or SSDPProtocol.create_multicast_socket(lan_address) listen_result: typing.Tuple = await loop.create_datagram_endpoint( diff --git a/aioupnp/upnp.py b/aioupnp/upnp.py index 6f0c114..05e5bc8 100644 --- a/aioupnp/upnp.py +++ b/aioupnp/upnp.py @@ -346,18 +346,7 @@ class UPnP: kwargs = kwargs or {} igd_args = igd_args timeout = int(timeout) - close_loop = False - try: - loop = asyncio.get_running_loop() - except RuntimeError: - loop = asyncio.new_event_loop() - asyncio.set_event_loop(loop) - close_loop = True - if not loop and not close_loop: - loop = asyncio.new_event_loop() - asyncio.set_event_loop(loop) - close_loop = True - + loop = asyncio.get_event_loop_policy().get_event_loop() fut: asyncio.Future = asyncio.Future() async def wrapper(): @@ -393,8 +382,7 @@ class UPnP: wrapper = lambda : None loop.run_until_complete(wrapper()) - if close_loop: - loop.close() + loop.close() try: result = fut.result() except UPnPError as err: