I do not really understand the multithreading in Python, therefore I need the advice of a person who can tell me the most efficient and the most correct variant of multithreading from the point of view of organizing flows.

As I did before. For example, I have a function to send an http request:

def http_request(): #тело функции class HTTPThread(threading.Thread): def run(self): try: http_request() except Exception, ex: pass for i in range(5): while True: t = HTTPThread() t.start() 

How correct or incorrect is this way to implement multithreading?

  • This can be said only by the example of a specific task. In this case, you create 5 threads in which you send a request, and therefore they are executed in parallel. Yes, if requests are executed for a long time, it will be faster than performing them sequentially. - Firepro
  • for i in range (5): while True: At this point an attempt is made to create an infinite number of streams. When you try to start, the OS will run out of memory and threads with a regular process launch after a few seconds. - cridnirk 2:57 pm
  • @cridnirk so I want to make sure that the requests are executed at regular intervals. Let's say every 10 seconds. And they were executed, for example, in 5 simultaneous streams. And so, to stop this cycle, I could using Ctrl + C. How can this be implemented? - JamesJGoodwin

3 answers 3

A simple compact example of using multithreading to work with HTTP requests (taken from here ):

 import Queue import threading import urllib2 def get_url(q, url): q.put(urllib2.urlopen(url).read()) urls = ["http://google.com", "http://yahoo.com"] q = Queue.Queue() for u in urls: t = threading.Thread(target=get_url, args=(q, u)) t.daemon = True t.start() s = q.get() print s 

The work with queues is illustrated here, thanks to which the results of calculations are returned from the generated flows to the main flow. Without the use of one of the synchronization mechanisms, the correct return of the results would be impossible. The queues are perfect for this.

It is worth adding that multithreading in Python ensures effective parallelization of only tasks waiting for completion of I / O operations (as is the case with waiting for an answer to an HTTP request). For computational tasks with a load on the central processor, there is no real load parallelization between threads due to the presence of GIL .

    Another interesting option is to use a pool of threads.

     from multiprocessing.dummy import Pool as ThreadPool try: from urllib.request import urlopen except ImportError: from urllib2 import urlopen def get_url(url): return urlopen(url).read() if __name__ == "__main__": urls = ["http://google.com", "http://yahoo.com"] with ThreadPool(processes=5) as pool: print(pool.map(get_url, urls)) 

      Here are the good 2 articles:

      Threading Basics in Python

      Learning to write multi-threaded and multi-process applications in Python