| 64 | |
| 65 | |
| 66 | class ThreadMap(BaseThread): |
| 67 | def __init__(self, fun, items, arg=None, concurrency=6): |
| 68 | super(ThreadMap, self).__init__(targets=items, concurrency=concurrency) |
| 69 | if not callable(fun): |
| 70 | raise TypeError("fun must be callable.") |
| 71 | |
| 72 | self._arg = arg |
| 73 | self._fun = fun |
| 74 | self._result_map = {} |
| 75 | |
| 76 | def work(self, item): |
| 77 | if self._arg: |
| 78 | result = self._fun(item, self._arg) |
| 79 | else: |
| 80 | result = self._fun(item) |
| 81 | |
| 82 | if result: |
| 83 | self._result_map[str(item)] = result |
| 84 | |
| 85 | def run(self): |
| 86 | self._run() |
| 87 | return self._result_map |
| 88 | |
| 89 | |
| 90 | def thread_map(fun, items, arg=None, concurrency=6): |