How to check worker status in Asyncio?

Francesco

I have to download few data as author , title etc. I want to download only two book at once so I create one task for each one. When the download is finished I need to know it.

If I use queue.join() I'll know when tasks are finished but I have to wait for both task, instead, I want to put new item on queue as soon as a worker become 'free'

How Do I know when a worker become available to get a new item ? Below, you find a short code to explain what I trying to do Thanks for any help

nTasks = 2
async def worker(name):
    while True:
        #Wait for new book item
        queue_item = await queue_.get()
    
        #Starts to download author, title etc...
        loop = asyncio.get_event_loop()
        task = loop.create_task(download_books(queue_item, file))

    queue_.task_done()

async def main():
try:
                #We create 2 task at once
                count = 0
                while ( count < nTasks):
                        #Gets the book file name
                        mediaGet = ....
                        #Put on queue
                        await queue_.put(mediaGet)                    
                        #Next download
                        count = count + 1
                contaTask = 0        
                
                #Wait until tasks are finished
                await queue_.join()
user4815162342

I want to put new item on queue as soon as a worker become 'free'

You shouldn't need to care when a worker becomes free - the whole point of having workers is that you have a fixed number of them (two in your case) and that they drain the queue as fast as they can. You shouldn't use create_task() inside the worker because then you spawn the task in the background and discard the worker limit.

The correct way to work with a queue can look like this:

async def worker(queue):
    while True:
        queue_item = await queue.get()
        await download_books(queue_item, file)
        queue.task_done()

async def main():
    queue = asyncio.Queue()
    # create two workers
    workers = [asyncio.create_task(worker(queue)) for _ in 2]
    # populate the queue
    for media in ...:
        await queue.put(media)
    # wait for the workers to do their jobs
    await queue.join()
    # cancel the now-idle workers
    for w in workers:
        w.cancel()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Check status of Celery worker

From Dev

How to check Resque worker status to determine whether it's dead or stale

From Dev

How to check status of AVPlayer?

From Dev

How to check migration status?

From Dev

How to check the namenode status?

From Dev

How to check the status of URL?

From Dev

How to check the status of URL?

From Dev

How to show the resque worker status to the end user?

From Dev

How to check if Worker Role is running in Azure Emulator

From Dev

Oracle:How to check index status?

From Dev

How to Check Status of Links in Bookmarks

From Dev

How to check Magento index status

From Dev

how to check the daemon running status

From Dev

How to get check status for check box in jquery

From Dev

how to check if asyncio loop has any associated sockets

From Dev

How to check SOLR cloud health status or making zookeeper send status

From Dev

(JAVA) How check status of Step in AWS EMR?

From Dev

how to check screen on/off status in onStop()?

From Dev

How to check on every request a user status (and redirect to it)

From Dev

How to check event scheduler status mysql

From Dev

How to check is repo is in "dirty" status asynchronously? (For prompt)

From Dev

How to check status of table in DynamoDB2?

From Dev

How to check the status of all git repositories at once?

From Dev

how to check the status of css through javascript

From Dev

How to check internet connection status with vibed?

From Dev

How to check .NET BlockingCollection status and wait for completion?

From Dev

How to check status of error code in csh?

From Dev

How to check replication snapshot agent status?

From Dev

How to check status of the user (subscribed or not) using sendy

Related Related

HotTag

Archive