blob: 5d79a3ced5d72e83b8758675dc01523eb97cf8cb (
about) (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
"""
pdf2image filename generators
"""
import uuid
import threading
class ThreadSafeGenerator(object):
"""Wrapper around generator that protects concurrent access"""
def __init__(self, gen):
self.gen = gen
self.lock = threading.Lock()
def __iter__(self):
return self
def __next__(self):
with self.lock:
return next(self.gen)
def threadsafe(f):
"""Decorator to make generator threadsafe. Fix #125"""
def g(*a, **kw):
return ThreadSafeGenerator(f(*a, **kw))
return g
@threadsafe
def uuid_generator():
"""Returns a UUID4"""
while True:
yield str(uuid.uuid4())
@threadsafe
def counter_generator(prefix="", suffix="", padding_goal=4):
"""Returns a joined prefix, iteration number, and suffix"""
i = 0
while True:
i += 1
yield str(prefix) + str(i).zfill(padding_goal) + str(suffix)
|