Total Complexity | 4 |
Total Lines | 19 |
Duplicated Lines | 0 % |
Coverage | 90% |
1 | # -*- coding: utf-8 -*- |
||
6 | 1 | class threadsafe_iter(object): |
|
7 | """ |
||
8 | Takes an iterator/generator and makes it thread-safe by serializing call to |
||
9 | the ``next`` method of given iterator/generator. |
||
10 | """ |
||
11 | |||
12 | 1 | def __init__(self, original): |
|
13 | 1 | self.original = original |
|
14 | 1 | self.lock = threading.Lock() |
|
15 | |||
16 | 1 | def __iter__(self): |
|
17 | return self |
||
18 | |||
19 | 1 | def __next__(self): |
|
20 | 1 | with self.lock: |
|
21 | 1 | return next(self.original) |
|
22 | |||
23 | # Python 3 compatibility |
||
24 | next = __next__ |
||
25 |