| 1 |
import unittest |
| 2 |
from threading import Thread |
| 3 |
|
| 4 |
from test.support import threading_helper |
| 5 |
|
| 6 |
|
| 7 |
class ZipThreading(unittest.TestCase): |
| 8 |
@staticmethod |
| 9 |
def work(enum): |
| 10 |
while True: |
| 11 |
try: |
| 12 |
next(enum) |
| 13 |
except StopIteration: |
| 14 |
break |
| 15 |
|
| 16 |
@threading_helper.reap_threads |
| 17 |
@threading_helper.requires_working_threading() |
| 18 |
def test_threading(self): |
| 19 |
number_of_threads = 8 |
| 20 |
number_of_iterations = 8 |
| 21 |
n = 40_000 |
| 22 |
enum = zip(range(n), range(n)) |
| 23 |
for _ in range(number_of_iterations): |
| 24 |
worker_threads = [] |
| 25 |
for ii in range(number_of_threads): |
| 26 |
worker_threads.append( |
| 27 |
Thread( |
| 28 |
target=self.work, |
| 29 |
args=[ |
| 30 |
enum, |
| 31 |
], |
| 32 |
) |
| 33 |
) |
| 34 |
for t in worker_threads: |
| 35 |
t.start() |
| 36 |
for t in worker_threads: |
| 37 |
t.join() |
| 38 |
|
| 39 |
|
| 40 |
if __name__ == "__main__": |
| 41 |
unittest.main() |