-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrecycler.py
54 lines (47 loc) · 1.92 KB
/
recycler.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
import numpy as np
from requests import session
from .core import CPUClient, GPUClient, HybridClient
from .temp import TempCPUWorker
from .errors import *
# Dump a client's attributes into a dictionary so that it can be used remotely.
def dump(c):
try:
return {
"_type": c.type,
"url": c.url,
"token": c.token,
"nickname": c.nickname,
"shard": c.shard if hasattr(c, 'shard') else None,
"start_id": str(c.start_id) if hasattr(c, 'start_id') else None,
"end_id": str(c.end_id) if hasattr(c, 'end_id') else None,
"shard_piece": c.shard_piece if hasattr(c, 'shard_piece') else None,
"wat": c.wat if hasattr(c, 'wat') else None,
"shards": c.shards if hasattr(c, 'shards') else None
}
except AttributeError as e:
raise DumpError(f"[crawling@home] unable to dump client: {e}")
# Load an existing client using its attributes. It's best to load using an existing dumpClient(): `loadClient(**dump)`
def load(_type=None, url=None, token=None, nickname=None, shard=None,
start_id=None, end_id=None, shard_piece=None, wat=None, shards=None):
if _type == "HYBRID":
c = HybridClient(*[None] * 2, _recycled=True)
elif _type == "CPU":
c = CPUClient(*[None] * 2, _recycled=True)
elif _type == "GPU":
c = GPUClient(*[None] * 2, _recycled=True)
elif _type == "FULLWAT":
c = TempCPUWorker(url, nickname, _recycled=True)
else:
raise ValueError(f"Invalid worker type: {_type}")
c.s = session()
c.type = _type
c.url = url
c.token = token
c.nickname = nickname
c.shard = shard
c.start_id = start_id if isinstance(start_id, np.int64) else np.int64(start_id)
c.end_id = end_id if isinstance(end_id, np.int64) else np.int64(end_id)
c.shard_piece = shard_piece
c.wat = wat
c.shards = shards
return c