Completed
Push — master ( efd649...05ee36 )
by Oleksandr
01:25
created

isotopic_logging.threadsafe_iter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Test Coverage

Coverage 90.91%
Metric Value
dl 0
loc 19
ccs 10
cts 11
cp 0.9091
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __next__() 0 3 2
A __iter__() 0 2 1
A __init__() 0 3 1
A next() 0 2 1
1
# -*- coding: utf-8 -*-
2
3 1
import threading
4
5
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 1
    def next(self):
24
        return self.__next__()
25