-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
42 lines (31 loc) · 976 Bytes
/
app.py
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
import string
import random
import json
import time
import threading
rlock = threading.RLock()
def event(data):
with rlock:
print(json.dumps(data), flush=True)
time.sleep(0.002)
def process_file(idx, file_name):
event({"file_name": file_name, "action": "STARTED_PROCESSING", "worker": idx})
time.sleep(random.randint(3, 10))
event({"file_name": file_name, "action": "FINISHED_PROCESSING", "worker": idx})
def producer(idx):
while True:
time.sleep(random.randint(3, 10))
file_name = "".join(
random.sample(string.digits + string.ascii_letters, 10)
) + ".txt"
process_file(idx, file_name)
time.sleep(random.randint(3, 10))
def main():
workers = []
for idx in range(10):
worker = threading.Thread(target=producer, args=(idx,), daemon=True)
worker.start()
workers.append(worker)
[w.join() for w in workers]
if __name__ == '__main__':
main()