forked from databrickslabs/migrate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread_safe_writer.py
More file actions
25 lines (20 loc) · 821 Bytes
/
thread_safe_writer.py
File metadata and controls
25 lines (20 loc) · 821 Bytes
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
import threading
class ThreadSafeWriter():
"""Class that ensures the thread-safe file write via the Synchronized Queue object.
For example, this class can be used by multiple threads to write to the same file safely.
This class is not useful when parallelization is done across multiple files.
Initialize by passing in the file open args.
e.g. writer = ThreadSafeWriter("file_to_write.txt", "w")
writer.write("content1")
writer.write("content2")
writer.close()
"""
def __init__(self, *args):
self.global_lock = threading.Lock()
self.filewriter = open(*args)
def write(self, data):
with self.global_lock:
self.filewriter.write(data)
self.filewriter.flush()
def close(self):
self.filewriter.close()