Python asyncio doesn't show any errors


superman

I am trying to get some data from thousands of URLs by using asyncio. Here is a brief overview of the design:

  1. QueuePopulate a bunch of URLs at once with one URLProducer
  2. generate a bunchConsumers
  3. Each Consumerkeeps fetching urls from Queueand sending GETrequests asynchronously
  4. Do some post-processing on the results
  5. Combine all processed results and return

Problem: asyncio It almost never shows if there are any errors, it just hangs silently without any errors. I put statements printeverywhere to detect my own problem, but it didn't help much.

Depending on the number of URLs entered and the number or limit of users, I may encounter the following errors:

  1. Task was destroyed but it is pending!
  2. task exception was never retrieved future: <Task finished coro=<consumer()
  3. aiohttp.client_exceptions.ServerDisconnectedError
  4. aiohttp.client_exceptions.ClientOSError: [WinError 10053] An established connection was aborted by the software in your host machine

Question: How to detect and handle exceptions in asyncio? How to try again without disturbing Queue?

Bellow is the code I wrote looking at various asynchronous code examples. Currently, there is an intentional error at the end of the def get_video_titlefunction . When running, nothing is displayed.

import asyncio
import aiohttp
import json
import re
import nest_asyncio
nest_asyncio.apply() # jupyter notebook throws errors without this


user_agent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36"

def get_video_title(data):
    match = re.search(r'window\[["\']ytInitialPlayerResponse["\']\]\s*=\s*(.*)', data)
    string = match[1].strip()[:-1]
    result = json.loads(string)
    return result['videoDetails']['TEST_ERROR'] # <---- should be 'title'

async def fetch(session, url, c):
    async with session.get(url, headers={"user-agent": user_agent}, raise_for_status=True, timeout=60) as r:
        print('---------Fetching', c)
        if r.status != 200:
            r.raise_for_status()
        return await r.text()

async def consumer(queue, session, responses):
    while True:
        try:
            i, url = await queue.get()
            print("Fetching from a queue", i)
            html_page = await fetch(session, url, i)

            print('+++Processing', i)
            result = get_video_title(html_page) # should raise an error here!
            responses.append(result)
            queue.task_done()

            print('+++Task Done', i)

        except (aiohttp.http_exceptions.HttpProcessingError, asyncio.TimeoutError) as e:
            print('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Error', i, type(e))
            await asyncio.sleep(1)
            queue.task_done()

async def produce(queue, urls):
    for i, url in enumerate(urls):
        print('Putting in a queue', i)
        await queue.put((i, url))

async def run(session, urls, consumer_num):
    queue, responses = asyncio.Queue(maxsize=2000), []

    print('[Making Consumers]')
    consumers = [asyncio.ensure_future(
        consumer(queue, session, responses)) 
                 for _ in range(consumer_num)]

    print('[Making Producer]')
    producer = await produce(queue=queue, urls=urls)

    print('[Joining queue]')
    await queue.join()

    print('[Cancelling]')
    for consumer_future in consumers:
        consumer_future.cancel()

    print('[Returning results]')
    return responses

async def main(loop, urls):
    print('Starting a Session')
    async with aiohttp.ClientSession(loop=loop, connector=aiohttp.TCPConnector(limit=300)) as session:
        print('Calling main function')
        posts = await run(session, urls, 100)
        print('Done')
        return posts


if __name__ == '__main__':
    urls = ['https://www.youtube.com/watch?v=dNQs_Bef_V8'] * 100
    loop = asyncio.get_event_loop()
    results = loop.run_until_complete(main(loop, urls))
username

The problem is that you consumerare only catching two very specific exceptions and in this case marking the task as completed. It will terminate the consumer if any other exception occurs (such as a network related exception). However, it is not detected runthat this is waiting queue.join()(effectively) for a consumer running in the background. That's why your program hangs - the queued items are never considered, the queue is never fully processed.

There are two ways to solve this problem, depending on what the program wants to do when it encounters an unexpected exception. If you want it to keep running, you can add a catch-all clause exceptto the consumer , such as:

        except Exception as e
            print('other error', e)
            queue.task_done()

The alternative is to propagate unhandled consumer exceptions to run. This must be arranged explicitly, but has the advantage of never allowing exceptions to be passed silently. ( See this article for details on this topic .) One way to achieve this is to wait and consumer at the same time. Since consumers are in an infinite loop, they only complete if an exception occurs.queue.join()

    print('[Joining queue]')
    # wait for either `queue.join()` to complete or a consumer to raise
    done, _ = await asyncio.wait([queue.join(), *consumers],
                                 return_when=asyncio.FIRST_COMPLETED)
    consumers_raised = set(done) & set(consumers)
    if consumers_raised:
        await consumers_raised.pop()  # propagate the exception

Question: How to detect and handle exceptions in asyncio?

Exceptions are propagated awaitlike any other code and detected and handled normally. Only special handling is required to catch exceptions leaked from "background" tasks such as consumer.

How can I retry without interrupting the queue?

You can call await queue.put((i, url))in a exceptblock . The item will be added to the back of the queue for the consumer to pick up. In this case, you only need the first snippet, and don't want to bother trying to propagate the exception consumerinto run.

Related


Python asyncio doesn't show any errors

superman I am trying to get some data from thousands of URLs by using asyncio. Here is a brief overview of the design: QueuePopulate a bunch of URLs at once with one URLProducer generate a bunchConsumers Each Consumerkeeps fetching urls from Queueand sending G

Python asyncio doesn't show any errors

superman I am trying to get some data from thousands of URLs by using asyncio. Here is a brief overview of the design: QueuePopulate a bunch of URLs at once with one URLProducer generate a bunchConsumers Each Consumerkeeps fetching urls from Queueand sending G

Python asyncio doesn't show any errors

superman I am trying to get some data from thousands of URLs by using asyncio. Here is a brief overview of the design: QueuePopulate a bunch of URLs at once with one URLProducer generate a bunchConsumers Each Consumerkeeps fetching urls from Queueand sending G

PHP doesn't show any errors

UserIsCorrupt I know this question has been asked many times in the past, but none of the solutions worked for me. Here is my PHP code: <?php function ?> This should generate an error. However, it just returns a 500 error and doesn't load. I tried the less

My python code doesn't run and doesn't show any errors

Ulysses Barbera > print('¡Hi, welcome!') > while(True): > option = input('''\n¿What you want do? ¡Select an option! > (A) Greet > (B) Add two numbers > (C) Exit''') > if option == 'A': > print('\n The developer sends his regards \n')

Electron window doesn't open but doesn't show any errors

Jeff Context: I 've seen this question, but it doesn't solve my problem. When I try to run my electron app, I don't get any errors and it seems to compile, but the electron window doesn't pop up. Reply: Date: 2019-05-08T03:02:22.036Z Hash: 3303fd48d099a538493f

ionic cordova doesn't build but doesn't show any errors

Tristan I just made a "new" ionic app, but I can't get Cordova to build the APK. I put the new quotes because I created a new project and copied over from the previous project's src. Something went horribly wrong with the dependencies of the plugin installed o

Ajax doesn't work. it doesn't show any errors

Ati I am using Ajax in my application. Previously it worked fine and checked many times. Not working now. I didn't change anything in this code. I checked the URL path and also checked the Jquery click event. It doesn't show any errors in the console log. $(".

Electron window doesn't open but doesn't show any errors

Jeff Context: I 've seen this question, but it doesn't solve my problem. When I try to run my electron app, I don't get any errors and it seems to compile, but the electron window doesn't pop up. Reply: Date: 2019-05-08T03:02:22.036Z Hash: 3303fd48d099a538493f

Electron window doesn't open but doesn't show any errors

Jeff Context: I 've seen this question, but it doesn't solve my problem. When I try to run my electron app, I don't get any errors and it seems to compile, but the electron window doesn't pop up. Reply: Date: 2019-05-08T03:02:22.036Z Hash: 3303fd48d099a538493f

ionic cordova doesn't build but doesn't show any errors

Tristan I just made a "new" ionic app, but I can't get Cordova to build the APK. I put the new quotes because I created a new project and copied over from the previous project's src. Something went horribly wrong with the dependencies of the plugin installed o

MSBuild won't deploy and doesn't show any errors

Mohammad Rostami Sehqli I am trying to publish my project using MSBuild. This is an order: "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\MSBuild.exe" D:\HamrahFarda\KhandeShow\KhandeShow.sln /t:UserService /p:DeployOnBuild=tru

MSBuild won't deploy and doesn't show any errors

Mohammad Rostami Sehqli I am trying to publish my project using MSBuild. This is an order: "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\MSBuild.exe" D:\HamrahFarda\KhandeShow\KhandeShow.sln /t:UserService /p:DeployOnBuild=tru

MSBuild won't deploy and doesn't show any errors

Mohammad Rostami Sehqli I am trying to publish my project using MSBuild. This is an order: "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\MSBuild.exe" D:\HamrahFarda\KhandeShow\KhandeShow.sln /t:UserService /p:DeployOnBuild=tru

MSBuild won't deploy and doesn't show any errors

Mohammad Rostami Sehqli I am trying to publish my project using MSBuild. This is an order: "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\MSBuild.exe" D:\HamrahFarda\KhandeShow\KhandeShow.sln /t:UserService /p:DeployOnBuild=tru

Intellij doesn't show any errors/suggestions on syntax

Bartosz_76 There are several similar threads, but none seem to contain an answer to my question. My IDE doesn't show any errors, doesn't give any suggestions, and doesn't import anything. example: public static String createInversion(ArrayList<String> splitNam

Why doesn't CPP Check show any errors?

Maug said to restore Monica this cppcheck --enable=style --inconclusive --check-config --xml --xml-version=2 -v -I.. -I../mocks -I../gmock -I../gtest -DUNIT_TEST ../src result <?xml version="1.0" encoding="UTF-8"?> <results version="2"> <cppcheck version="1

Django form doesn't show any errors and is invalid

Theo Lavaux To create a form on my website, I create some models corresponding to fields. Then I created ModelForms from them, some views and templates. My problem is that I never see the form error in the first place, and second, the form for this particular

Intellij doesn't show any errors/suggestions on syntax

Bartosz_76 There are several similar threads, but none seem to contain an answer to my question. My IDE doesn't show any errors, doesn't give any suggestions, and doesn't import anything. example: public static String createInversion(ArrayList<String> splitNam

Recycler view doesn't show any items without errors

Majiduddin Alban hi i'm new to android devolpment and i got an error that can't be resolved for a week and the error is that no items are shown in my recycler view, i really don't know why i read all the questions and documentation on stackoverflow Relevant bu

XMLHttp request post misses URL and doesn't show any errors

Carlisle Screenshot of web log I wrote an XMLHttp request POST method in place of form post. While debugging it works fine and doesn't show any errors, but also doesn't hit the URL. var http = new XMLHttpRequest(); var url = "https://localhost:8083/unique/test