![]() |
|
[Python] Multithreading in Python - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [Python] Multithreading in Python (/showthread.php?tid=6038310) |
Multithreading in Python - Aexone ✔ - Jan 5, 2020 Hello all, I've coded a script that goes in a .txt file and for each line it make a request on a website and try to scrap something. It works good but i've a lot of site to check so i want to use multithreading. My script works like this : def firstFunc(): with open(filename) as file: for url in urls: secondFunc(url) #this function make the requests, scrap and write on a file if ok So i want to know how can I setup the multithreading. I tried but i got duplicate results, it means that instead of having a queue of url to pass to the different thread, they all do the same requests. For example with 3 threads, instead of having : google -> ok yahoo -> ok yandex -> ok I have : google -> ok google -> ok google -> ok yahoo -> ok yahoo -> ok yahoo ->ok ..... Anyone can help me ? Thank you ! RE: Multithreading in Python - roosiinfidel09 - Jan 5, 2020 start here. https://www.geeksforgeeks.org/multithreading-python-set-1/ RE: Multithreading in Python - lnvisible - Jan 5, 2020 You can insert code with tags Code def firstFunc():RE: Multithreading in Python - Aexone ✔ - Jan 5, 2020 (Jan 5, 2020 11:02 AM)roosiinfidel09 Wrote: start here. Same problem... i don't know why but the second thread don't continue with the other emails but redo what the first thread do https://prnt.sc/qjpok6 RE: Multithreading in Python - roosiinfidel09 - Jan 5, 2020 (Jan 5, 2020 12:06 PM)Aexone ✔ Wrote: Same problem... i don't know why but the second thread don't continue with the other emails but redo what the first thread do My point was...You must be looking for your script specific solution. That is always a bad idea. Understand how multi-threading works...then try on your own...if you really can't then post the question. Also, make sure you put in words which make you appear knowledgeable about the topic. Lots of assholes here will give you -3 popularity without reading your question because you didn't include fancy words. RE: Multithreading in Python - Nomad.Null - Jan 5, 2020 Sounds like the fundamental problem you're having is that you are not logically separating the list and distributing them to each thread (or more accurately, each process, since multi-threading still isn't a thing in python). Easiest solution to your problem is to slice your list of urls equally into separate lists and pass them to each process. Better yet, you should be using a set to start with to ensure there are no duplicate urls. Using a set will automatically de-duplicate your list of urls. Hope that helps. RE: Multithreading in Python - Lost Soul - Jun 2, 2020 (Jan 5, 2020 10:39 AM)Aexone ✔ Wrote: Hello all, I can help you, it is so easy. it will be ready in like 5 minutes haha. Pm me if u still interested. RE: Multithreading in Python - vorticalbox - Jun 2, 2020 I think async is better than threading because of the GIL and multi processing is memory intensive as each process gets the copy of the list. here is an async script to get the HTML title from urls PHP Code RE: Multithreading in Python - synchronizing - Jun 2, 2020 (Jun 2, 2020 05:34 AM)vorticalbox Wrote: I think async is better than threading because of the GIL and multi processing is memory intensive as each process gets the copy of the list. @OP, asyncio is definitely the better choice when it comes to scraping multiple websites. To explain further, I'm adapting an answer I wrote elsewhere unto here. @vorticalbox used http3, and I used aiohttp for sending GET requests asynchronously. It's your choice which you prefer, but both should generally work in the same manner. You should look into asynchronous programming. Different from thread, asynchronous code runs in the same thread, but it runs inside an event loop. This event loop automatically switches context between different operations when the Python keyword await is present. In other words, think of scraping websites as the following: Code client sends request -> ... waiting for server reply ... <- server repliesSending a request is an operation that takes a very small amount of time and consumes almost no resources. The real time consumer is waiting for the server to respond, and then processing the server's reply. If instead we do something that resembles the following: Code client sends request -> switch operation -> ... wait ... <- server repliesThen we can minimize our time waiting for the server to reply, and instead already be shooting the next request over. In other words, what we can effectively do is tell Python to send the request, and then instantly switch to a different part of our code that sends another request, and then another part that sends another request, and so on. When all the requests are sent, we can come back and start interpreting the individual server replies. There is a lot of references online on how to program asynchronously in Python (using the built-in asynchro module + PyPi installable aiohttp module), and I would suggest Googling away. Here is a code sample that will take less than 4 seconds to scrape over 100 websites (note that this scales extremely well, and 4 seconds is actually due to the print statements... without, its actually closer to 2 seconds): Code import asyncioOutputs: Code Successfully got url http://www.google.com.br with response of length 12188.As you can see 100 websites from across the world were successfully reached (with or without https) in about 4 seconds with aiohttp on my internet connection. Keep in mind the following can slow down the program by a few ms:
The example above has both instances of the above, and therefore it is arguably the least-optimized way of doing what you have asked. However, I do believe it is a great start for what you are looking for. RE: Multithreading in Python - Lindros - Jun 3, 2020 You can look into using a Thread Pool Executor if you want a different method. It does all of the thread managing for you and is pretty easy to use. |