From 95ba1444328a1b2df52210bf72f09348bcf491ca Mon Sep 17 00:00:00 2001 From: Ziloka <50429450+ziloka@users.noreply.github.com> Date: Wed, 17 Jan 2024 10:10:24 -0500 Subject: [PATCH] prevent asyncio hanging --- .python-version | 1 + proxybroker/api.py | 2 +- proxybroker/checker.py | 2 +- proxybroker/cli.py | 3 ++- proxybroker/judge.py | 2 +- proxybroker/providers.py | 2 +- proxybroker/resolver.py | 2 +- proxybroker/server.py | 2 +- pyproject.toml | 2 +- 9 files changed, 10 insertions(+), 8 deletions(-) create mode 100644 .python-version diff --git a/.python-version b/.python-version new file mode 100644 index 00000000..92536a9e --- /dev/null +++ b/.python-version @@ -0,0 +1 @@ +3.12.0 diff --git a/proxybroker/api.py b/proxybroker/api.py index 71b61fe0..135b61e0 100644 --- a/proxybroker/api.py +++ b/proxybroker/api.py @@ -64,7 +64,7 @@ def __init__( stop_broker_on_sigint=True, **kwargs, ): - self._loop = loop or asyncio.get_event_loop_policy().get_event_loop() + self._loop = loop self._proxies = queue or asyncio.Queue() self._resolver = Resolver(loop=self._loop) self._timeout = timeout diff --git a/proxybroker/checker.py b/proxybroker/checker.py index bedad3f5..a510db37 100644 --- a/proxybroker/checker.py +++ b/proxybroker/checker.py @@ -43,7 +43,7 @@ def __init__( self._strict = strict self._dnsbl = dnsbl or [] self._types = types or {} - self._loop = loop or asyncio.get_event_loop() + self._loop = loop self._resolver = Resolver(loop=self._loop) self._req_http_proto = not types or bool( diff --git a/proxybroker/cli.py b/proxybroker/cli.py index 22bb3bd8..1ab4e1e1 100644 --- a/proxybroker/cli.py +++ b/proxybroker/cli.py @@ -387,7 +387,7 @@ def cli(args=sys.argv[1:]): ns.types.remove('HTTP') ns.types.append(('HTTP', ns.anon_lvl)) - loop = asyncio.get_event_loop_policy().get_event_loop() + loop = asyncio.new_event_loop() proxies = asyncio.Queue() broker = Broker( proxies, @@ -443,6 +443,7 @@ def cli(args=sys.argv[1:]): try: if tasks: + asyncio.set_event_loop(loop) loop.run_until_complete(asyncio.gather(*tasks)) if ns.show_stats: broker.show_stats(verbose=True) diff --git a/proxybroker/judge.py b/proxybroker/judge.py index d7dfe84d..4f1bbe8a 100644 --- a/proxybroker/judge.py +++ b/proxybroker/judge.py @@ -29,7 +29,7 @@ def __init__(self, url, timeout=8, verify_ssl=False, loop=None): self.marks = {'via': 0, 'proxy': 0} self.timeout = timeout self.verify_ssl = verify_ssl - self._loop = loop or asyncio.get_event_loop() + self._loop = loop self._resolver = Resolver(loop=self._loop) def __repr__(self): diff --git a/proxybroker/providers.py b/proxybroker/providers.py index 17b67ab5..6e9adf85 100644 --- a/proxybroker/providers.py +++ b/proxybroker/providers.py @@ -45,7 +45,7 @@ def __init__( self._proxies = set() # concurrent connections on the current provider self._sem_provider = asyncio.Semaphore(max_conn) - self._loop = loop or asyncio.get_event_loop() + self._loop = loop @property def proxies(self): diff --git a/proxybroker/resolver.py b/proxybroker/resolver.py index 10afb42e..5f945828 100644 --- a/proxybroker/resolver.py +++ b/proxybroker/resolver.py @@ -44,7 +44,7 @@ class Resolver: def __init__(self, timeout=5, loop=None): self._timeout = timeout - self._loop = loop or asyncio.get_event_loop() + self._loop = loop self._resolver = aiodns.DNSResolver(loop=self._loop) @staticmethod diff --git a/proxybroker/server.py b/proxybroker/server.py index b969c5e2..16c2c2c9 100644 --- a/proxybroker/server.py +++ b/proxybroker/server.py @@ -131,7 +131,7 @@ def __init__( ): self.host = host self.port = int(port) - self._loop = loop or asyncio.get_event_loop() + self._loop = loop self._timeout = timeout self._max_tries = max_tries self._backlog = backlog diff --git a/pyproject.toml b/pyproject.toml index f8a9ffa0..2f596975 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -70,7 +70,7 @@ exclude = ''' | build | dist ) -) +).0 ''' [tool.pytest.ini_options]