Repository navigation
Expand file tree
/
Copy pathstarter_code.py
More file actions
52 lines (41 loc) · 1.51 KB
/
Copy pathstarter_code.py
File metadata and controls
52 lines (41 loc) · 1.51 KB
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
47
48
49
50
51
52
import mock_db
import uuid
from worker import worker_main
from threading import Thread
def lock_is_free():
"""
CHANGE ME, POSSIBLY MY ARGS
Return whether the lock is free
"""
return True
def attempt_run_worker(worker_hash, give_up_after, db, retry_interval):
"""
CHANGE MY IMPLEMENTATION, BUT NOT FUNCTION SIGNATURE
Run the worker from worker.py by calling worker_main
Args:
worker_hash: a random string we will use as an id for the running worker
give_up_after: if the worker has not run after this many seconds, give up
db: an instance of MockDB
retry_interval: continually poll the locking system after this many seconds
until the lock is free, unless we have been trying for more
than give_up_after seconds
"""
if lock_is_free():
worker_main(worker_hash, db)
if __name__ == "__main__":
"""
DO NOT MODIFY
Main function that runs the worker five times, each on a new thread
We have provided hard-coded values for how often the worker should retry
grabbing lock and when it should give up. Use these as you see fit, but
you should not need to change them
"""
db = mock_db.DB()
threads = []
for _ in range(25):
t = Thread(target=attempt_run_worker, args=(uuid.uuid1(), 2000, db, 0.1))
threads.append(t)
for t in threads:
t.start()
for t in threads:
t.join()