-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.py
114 lines (86 loc) · 3.57 KB
/
common.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import calendar
import json
import logging
import os
import time
from _socket import gethostname
from copy import copy
from datetime import datetime, timedelta
from random import uniform
from typing import Optional, Tuple
from uuid import uuid4
def setup_logger() -> None:
logging.basicConfig(format="%(asctime)s UTC | %(levelname)s | %(process)d | %(message)s", level=logging.INFO)
logging.Formatter.converter = time.gmtime
def dt_to_timestamp(dt: datetime) -> Tuple[int, int]:
return calendar.timegm(dt.utctimetuple()), dt.microsecond
def timestamp_to_dt(timestamp: int, microseconds: int) -> datetime:
return datetime.utcfromtimestamp(timestamp) + timedelta(microseconds=microseconds)
def curr_proc_host() -> str:
return str(os.getpid()) + "@" + gethostname()
class Task:
def __init__(self, uuid: str, sender: str, sent: datetime, param: float) -> None:
self.uuid = uuid
self.sender = sender
self.param = param
self.sent = sent
@classmethod
def new(cls, param: float) -> 'Task':
return Task(uuid=str(uuid4()), sender=curr_proc_host(), sent=datetime.utcnow(), param=param)
def to_json(self) -> str:
as_dict = copy(self.__dict__)
as_dict["sent_dt"], as_dict["sent_us"] = dt_to_timestamp(as_dict.pop("sent"))
return json.dumps(as_dict)
@classmethod
def from_json(cls, as_json: str) -> 'Task':
as_dict = json.loads(as_json)
as_dict["sent"] = timestamp_to_dt(as_dict.pop("sent_dt"), as_dict.pop("sent_us"))
return Task(**as_dict)
def __repr__(self):
return self.to_json()
def __str__(self):
return self.to_json()
def __hash__(self):
return self.uuid
def __eq__(self, other):
return self.uuid == other.uuid
class DoneTask(Task):
def __init__(self, uuid: str, sender: str, sent: datetime, param: float, got: datetime, done: datetime,
worker: str, result: float) -> None:
super().__init__(uuid, sender, sent, param)
self.worker = worker
self.result = result
self.done = done
self.got = got
def to_json(self) -> str:
as_dict = copy(self.__dict__)
as_dict["sent_dt"], as_dict["sent_us"] = dt_to_timestamp(as_dict.pop("sent"))
as_dict["got_dt"], as_dict["got_us"] = dt_to_timestamp(as_dict.pop("got"))
as_dict["done_dt"], as_dict["done_us"] = dt_to_timestamp(as_dict.pop("done"))
return json.dumps(as_dict)
@classmethod
def from_json(cls, as_json: str) -> 'DoneTask':
as_dict = json.loads(as_json)
as_dict["sent"] = timestamp_to_dt(as_dict.pop("sent_dt"), as_dict.pop("sent_us"))
as_dict["done"] = timestamp_to_dt(as_dict.pop("done_dt"), as_dict.pop("done_us"))
as_dict["got"] = timestamp_to_dt(as_dict.pop("got_dt"), as_dict.pop("got_us"))
return DoneTask(**as_dict)
def took(self) -> float:
return (self.done - self.got).total_seconds()
def __repr__(self):
return self.to_json()
def __str__(self):
return self.to_json()
def __hash__(self):
return self.uuid
def __eq__(self, other):
return self.uuid == other.uuid
def do(task: Task) -> Optional[DoneTask]:
got = datetime.utcnow()
result, ops, op_limit = 0, 0, int(task.param * (10 ** 6))
while ops < op_limit:
result += 1
ops += 1
time.sleep(uniform(0., 0.1)) # simulate I/O
return DoneTask(uuid=task.uuid, sender=task.sender, sent=task.sent, param=task.param, got=got,
worker=curr_proc_host(), done=datetime.utcnow(), result=result)