| 1 |
import unittest |
| 2 |
from threading import Thread |
| 3 |
from operator import methodcaller |
| 4 |
|
| 5 |
|
| 6 |
class TestMethodcaller(unittest.TestCase): |
| 7 |
def test_methodcaller_threading(self): |
| 8 |
number_of_threads = 10 |
| 9 |
size = 4_000 |
| 10 |
|
| 11 |
mc = methodcaller("append", 2) |
| 12 |
|
| 13 |
def work(mc, l, ii): |
| 14 |
for _ in range(ii): |
| 15 |
mc(l) |
| 16 |
|
| 17 |
worker_threads = [] |
| 18 |
lists = [] |
| 19 |
for ii in range(number_of_threads): |
| 20 |
l = [] |
| 21 |
lists.append(l) |
| 22 |
worker_threads.append(Thread(target=work, args=[mc, l, size])) |
| 23 |
for t in worker_threads: |
| 24 |
t.start() |
| 25 |
for t in worker_threads: |
| 26 |
t.join() |
| 27 |
for l in lists: |
| 28 |
assert len(l) == size |
| 29 |
|
| 30 |
|
| 31 |
if __name__ == "__main__": |
| 32 |
unittest.main() |