0

i am running a script which takes in a url and downloads the file locally, and then passes the filename as an argument to a function.The problem is doing this takes a lot of time.So i tried using Threadpool,but this does not give any improvements.Am i doing this wrong, here is what it looks like.

pool = ThreadPool(processes=8)
ocr_result = pool.apply_async(download_file, (url,))
file_name = ocr_result.get()
async_result = pool.apply_async(return_label, (file_name,))
prediction, prediction_list  = async_result.get() 

Any suggestions would be really helpful. Thanks in advance.

6
  • 1
    I recommend you use asyncio + aiohttp Commented Apr 17, 2019 at 6:11
  • @hansolo Thanks,ill check it out, Do you have sample code? Commented Apr 17, 2019 at 6:16
  • This should get you started Commented Apr 17, 2019 at 6:17
  • okay thanks , ill look into it. Commented Apr 17, 2019 at 6:18
  • You are not using multiprocessing. You are using the pool, but you are running just one task synchronously. Commented Apr 17, 2019 at 6:21

1 Answer 1

1

As suggested in comment, there's a sample using aiohttp and asyncio:

def main():
    # limit concurrency
    loop = asyncio.get_event_loop()
    connector = aiohttp.TCPConnector(limit=100)

    # login if required
    async with aiohttp.ClientSession(loop=loop, connector=connector) as sess:
        async with sess.post(
                LOGIN_URL, data=payload) as resp:

            # ensure login success
            assert resp.status == 200
            for url in download_links:
                await download(url, sess)

And your download function looks like:

async def download(url, sess):
    async with sess.get(url) as resp:
        if resp.status == 200:
            # post process

Finally use a main loop:

loop = asyncio.get_event_loop()
loop.run_until_complete(main())
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your time

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.