Skip to content
Advertisement

making multiple request at the same time with the module requests and print the output on excel(python)

Hi this is quite a complicated question From this script:

JavaScript

The code by itself do what is supposed to do (getting the data:foward eps,peg ratio,sector and paste them on an excel file)but the issue is that it takes a lot of time and the list stock is quite long(2531 elements) is there a way to make this code more efficent or faster?

I have attemped to follow the instruction from this video:https://www.youtube.com/watch?v=nFn4_nA_yk8 But i still need to write the information of any single stock in the excel file is there a way i can optimize all of this ? Maybe by sending multiple request at the same time and write the data on the excel on a different time? The only end goal is to make the whole process as fast as possible. Thanks in advance(if you need any other information leave a comment i will answer as soon as possible)

Advertisement

Answer

First you have to put code in function

JavaScript

And next you can run it in old way

JavaScript

and this gives me time ~15s, (but sometimes even ~32s)

And now you can use threading to run the same function with different values at the same time.

Because thread can’t return result directly so I use global dictionary results for this (because threads share memory).

JavaScript

And this gives me time ~6s

For more stocks it would be better to use Threading.Pool so it would run only few threads at the same time because running 2000+ threads at the same time is not good idea.


Full working code

JavaScript

Version with Pool

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement