constverum / ProxyBroker

Proxy [Finder | Checker | Server]. HTTP(S) & SOCKS :performing_arts:
http://proxybroker.readthedocs.io
Apache License 2.0
3.83k stars 1.08k forks source link

Error with create new loop #162

Closed bakaInc closed 4 years ago

bakaInc commented 4 years ago

I need to use a new loop in project so when i use asyncio with new loop i get error the code work fine if not attached new loop to asyncio

When run with

loop = asyncio.new_event_loop()    
asyncio.set_event_loop(loop)

i got warnings and no proxy print, program don't stop

 DeprecationWarning: The object should be created from async function
  headers=get_headers(), cookies=self._cookies, loop=self._loop
C:\ProgramData\Anaconda3\envs\pyqt37\lib\site-packages\aiohttp\connector.py:731: DeprecationWarning: The object should be created from async function
  loop=loop)
C:\ProgramData\Anaconda3\envs\pyqt37\lib\site-packages\aiohttp\cookiejar.py:55: DeprecationWarning: The object should be created from async function
  super().__init__(loop=loop)
import asyncio
from proxybroker import Broker

async def show(proxies):
    while True:
        proxy = await proxies.get()
        if proxy is None: break
        print('Found proxy: %s' % proxy)

async def main_call(loop_):
    proxies = asyncio.Queue()
    broker = Broker(proxies, loop=loop_)
    tasks = await asyncio.gather(
        broker.find(types=['HTTP', 'HTTPS'], limit=10, loop=loop_),
        show(proxies))

    loop = asyncio.get_event_loop()
    loop.run_until_complete(tasks)

if __name__ == '__main__':
    #uncomment to get error
    #loop = asyncio.new_event_loop()
    #asyncio.set_event_loop(loop)

    loop = asyncio.get_event_loop()
    loop.run_until_complete(main_call(loop))
bakaInc commented 4 years ago

same as https://github.com/constverum/ProxyBroker/issues/135

kitty-eu-org commented 1 year ago

@bakaInc This problem still exists, I don't know if there is any problem? my code A is:

import asyncio
from proxybroker2 import Broker
import logging

logging.basicConfig(level=logging.INFO)

async def find_proxy():
    loop = asyncio.get_event_loop()
    queue = asyncio.Queue()
    broker = Broker(queue, loop=loop)
    await broker.find(types=["HTTP", "HTTPS"], limit=20, lvl="High", strict=True)
    while broker._all_tasks:
        await asyncio.sleep(1)
    if broker:
        broker.stop()

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(find_proxy())
    # asyncio.run(find_proxy())

This code works fine

my code B is:

import asyncio
from proxybroker2 import Broker
import logging

logging.basicConfig(level=logging.INFO)

async def find_proxy():
    loop = asyncio.get_event_loop()
    queue = asyncio.Queue()
    broker = Broker(queue, loop=loop)
    await broker.find(types=["HTTP", "HTTPS"], limit=20, lvl="High", strict=True)
    while broker._all_tasks:
        await asyncio.sleep(1)
    if broker:
        broker.stop()

if __name__ == '__main__':
    # loop = asyncio.get_event_loop()
    # loop.run_until_complete(find_proxy())
    asyncio.run(find_proxy())

This code stuck warning: image