Blame view

4_100k_threads.py 506 Bytes
444c887a   Lars Tiede   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  #!/usr/bin/env python
  import threading
  import time
  
  finished_count = 0
  finished_count_lock = threading.Lock()
  
  def slow_op(n):
      global finished_count
      time.sleep(1)
      with finished_count_lock:
          finished_count += 1
  
  def main():
      threads = [
              threading.Thread(target=slow_op, args=(i,))
              for i in range(100 * 1000)
              ]
      for t in threads:
          t.start()
      for t in threads:
          t.join()
  
  main()
  print("finished_count = {}".format(finished_count))