-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathmain.py
More file actions
78 lines (66 loc) · 2.36 KB
/
main.py
File metadata and controls
78 lines (66 loc) · 2.36 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
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
from typing import Awaitable
import random
import copy
from loguru import logger
from tqdm import tqdm
from asyncio import (
AbstractEventLoop,
get_event_loop,
create_task,
Semaphore,
gather,
sleep,
)
from src.utils.mappings import module_handlers
from config import *
from src.utils.helper import (
private_keys,
active_module,
)
async def process_private_key(private_key: str, pbar: tqdm, thread_num: int, semaphore: Semaphore) -> bool:
tasks = []
if RANDOMIZE is True:
modules = copy.copy(active_module)
random.shuffle(modules)
else:
modules = active_module
async with semaphore:
for pattern in modules:
task = create_task(module_handlers[pattern](private_key, pbar))
tasks.append(task)
time_to_sleep = random.randint(MIN_PAUSE, MAX_PAUSE)
logger.info(f'Thread {thread_num}: Sleeping {time_to_sleep} seconds...')
await sleep(time_to_sleep)
await task
await gather(*tasks)
return all(task.done() for task in tasks)
async def main(loop: AbstractEventLoop) -> None:
num_threads = min(NUM_THREADS, len(private_keys))
semaphore = Semaphore(num_threads)
if not RUN_FOREVER:
tasks = []
thread_num = 1
for private_key in private_keys:
task = loop.create_task(process_private_key(private_key, pbar, thread_num, semaphore))
tasks.append(task)
thread_num += 1
await gather(*tasks)
completed = {private_key: task_result for private_key, task_result in zip(private_keys, tasks)}
if all(completed.values()):
return
while RUN_FOREVER:
tasks = []
thread_num = 1
for private_key in private_keys:
task = loop.create_task(process_private_key(private_key, pbar, thread_num, semaphore))
tasks.append(task)
thread_num += 1
await gather(*tasks)
def start_event_loop(awaitable: Awaitable[object], loop: AbstractEventLoop) -> None:
loop.run_until_complete(awaitable)
if __name__ == '__main__':
with tqdm(total=len(private_keys)) as pbar:
async def tracked_main():
await main(get_event_loop())
start_event_loop(tracked_main(), get_event_loop())
pbar.close()