asyncip application hangs

Yaşar Arabacı yasar11732 at gmail.com
Tue Jul 22 00:34:57 CEST 2014


2014-07-22 1:19 GMT+03:00 Yaşar Arabacı <yasar11732 at gmail.com>:
> This program is supposed to give me status codes for web pages that
> are found on my sitemap.xml file. But program hangs as Tasks wait for
> getting something out of the Queue. I think it has something to do
> with how I am using asyncio.Queue, but I couldn't figure out what am I
> doing wrong here. Can anyone help me with that?

Ok, I figured out what was wrong with my code.

Since asyncio.Queue.put() is a coroutine, I should have wrapped those
with asyncio.Task in order for them to start executing.

Therefore, this works;

    q = asyncio.Queue()
    loop = asyncio.get_event_loop()

    for i in range(num_coroutines):  # start 10 tasks
        tasks.append(asyncio.Task(print_status_code(q)))

    for loc in soup.find_all('loc'):
        asyncio.Task(q.put(loc.string))

    for i in range(num_coroutines):  # Put poison pil.
        asyncio.Task(q.put(None))

    print("%i items in queue" % q.qsize())

    loop.run_until_complete(asyncio.wait(tasks))

-- 
http://ysar.net/



More information about the Python-list mailing list