Python|Threading: Thread-based parallelism for beginner - 图1
© thepythoncode.com

由於語法渲染問題而影響閱讀體驗, 請移步博客閱讀~
本文GitPage地址

Tutorial from RealPython[1]

Python threading allows you to have different parts of your program run concurrently and can simplify your design. If you’ve got some experience in Python and want to speed up your program using threads, then this tutorial is for you[1:1]

What’s a Threading?

Threading is a separate flow of execution. In bash, you can run a background process by adding a symbol & at the end of commands. In python, we can achieve this with the example below.

As you might expect, if you run the codes below, thread_function(3) will be started at the end of thread_function(4)

  1. import threading
  2. import time
  3. def thread_function(Time):
  4. for i in range(Time):
  5. time.sleep(i)
  6. print(i)
  7. thread_function(4)
  8. thread_function(3)
  1. 0
  2. 1
  3. 2
  4. 3
  5. 0
  6. 1
  7. 2

But if we execute it in a threading or background process, they can run simultaneously.

  1. - thread_function(4)
  2. + x = threading.Thread(target=thread_function, args=(4,))
  3. + x.start()
  4. + # x.join()
  5. thread_function(3)
  1. 0
  2. 0
  3. 1
  4. 1
  5. 2
  6. 2
  7. 3

This is how we expect the background process, or daemon threads, to be performed.

join()

Some times, we’d like waiting for the threads. By doing so, we can call .join() to do so.

By uncomment x.join(), we can get the same result as the first.

  1. import threading
  2. import time
  3. def thread_function(Time):
  4. for i in range(Time):
  5. time.sleep(i)
  6. print(i)
  7. x = threading.Thread(target=thread_function, args=(4,))
  8. x.start()
  9. x.join()
  10. thread_function(3)
  1. 0
  2. 1
  3. 2
  4. 3
  5. 0
  6. 1
  7. 2

Working in a loop

  1. threads = list()
  2. for Time in range(5):
  3. x = threading.Thread(target=thread_function, args=(Time,))
  4. threads.append(x)
  5. x.start()
  1. 0
  2. 0
  3. 0
  4. 0
  5. 1
  6. 1
  7. 1
  8. 2
  9. 2
  10. 3

ThreadPoolExecutor

  1. import threading
  2. import time
  3. import concurrent.futures
  4. def thread_function(Time):
  5. for i in range(Time):
  6. time.sleep(i)
  7. print(i,"from", Time)
  8. print(Time, "is Down")
  9. if __name__ == "__main__":
  10. with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor:
  11. executor.map(thread_function, range(10))
  1. 0 is Down
  2. 0 from 1
  3. 0 from 2
  4. 1 is Down
  5. 0 from 3
  6. 0 from 4
  7. 1 from 2
  8. 2 is Down
  9. 0 from 5
  10. 1 from 4
  11. 1 from 3
  12. 1 from 5
  13. 2 from 4
  14. 2 from 3
  15. 3 is Down
  16. 0 from 6
  17. 1 from 6
  18. 2 from 5
  19. 3 from 4
  20. 4 is Down
  21. 0 from 7
  22. 2 from 6
  23. 1 from 7
  24. 3 from 5
  25. 2 from 7
  26. 3 from 6
  27. 4 from 5
  28. 5 is Down
  29. 0 from 8
  30. 3 from 7
  31. 1 from 8
  32. 4 from 6
  33. 2 from 8
  34. 4 from 7
  35. 3 from 8
  36. 5 from 6
  37. 6 is Down
  38. 0 from 9
  39. 1 from 9
  40. 5 from 7
  41. 2 from 9
  42. 4 from 8
  43. 3 from 9
  44. 5 from 8
  45. 6 from 7
  46. 7 is Down
  47. 4 from 9
  48. 6 from 8
  49. 5 from 9
  50. 6 from 9
  51. 7 from 8
  52. 8 is Down
  53. 7 from 9
  54. 8 from 9
  55. 9 is Down

The code creates a ThreadPoolExecutor as a context manager, telling it how many worker threads it wants in the pool. It then uses .map() to step through an iterable of things, in your case range(3), passing each one to a thread in the pool.[1:2]

TEST

  1. import threading, time
  2. def sleep():
  3. time.sleep(30)
  4. print("sleep is down")
  5. x = threading.Thread(target=test, args=())
  6. x.start()
  7. x = threading.Thread(target=test, args=())
  8. x.is_alive()

Enjoy~

本文由Python腳本GitHub/語雀自動更新

由於語法渲染問題而影響閱讀體驗, 請移步博客閱讀~
本文GitPage地址

GitHub: Karobben
Blog:Karobben
BiliBili:史上最不正經的生物狗


  1. https://realpython.com/intro-to-python-threading/ ↩︎ ↩︎ ↩︎