How can I use threading in Python?

前端 未结 19 2655
迷失自我
迷失自我 2020-11-21 04:54

I am trying to understand threading in Python. I\'ve looked at the documentation and examples, but quite frankly, many examples are overly sophisticated and I\'m having trou

19条回答
  •  梦如初夏
    2020-11-21 05:19

    NOTE: For actual parallelization in Python, you should use the multiprocessing module to fork multiple processes that execute in parallel (due to the global interpreter lock, Python threads provide interleaving, but they are in fact executed serially, not in parallel, and are only useful when interleaving I/O operations).

    However, if you are merely looking for interleaving (or are doing I/O operations that can be parallelized despite the global interpreter lock), then the threading module is the place to start. As a really simple example, let's consider the problem of summing a large range by summing subranges in parallel:

    import threading
    
    class SummingThread(threading.Thread):
         def __init__(self,low,high):
             super(SummingThread, self).__init__()
             self.low=low
             self.high=high
             self.total=0
    
         def run(self):
             for i in range(self.low,self.high):
                 self.total+=i
    
    
    thread1 = SummingThread(0,500000)
    thread2 = SummingThread(500000,1000000)
    thread1.start() # This actually causes the thread to run
    thread2.start()
    thread1.join()  # This waits until the thread has completed
    thread2.join()
    # At this point, both threads have completed
    result = thread1.total + thread2.total
    print result
    

    Note that the above is a very stupid example, as it does absolutely no I/O and will be executed serially albeit interleaved (with the added overhead of context switching) in CPython due to the global interpreter lock.

提交回复
热议问题