-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcore.py
43 lines (38 loc) · 990 Bytes
/
core.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
import os
import threading
from tqdm import tqdm
import json
def create_dir(dir):
"""Create the dir if the dir does not exists
"""
if not os.path.exists(dir):
os.makedirs(dir)
return dir
def multi_threading(queue, number_of_threads, task):
pbar = tqdm(total=len(queue))
def pop_queue():
if len(queue) > 0:
return queue.pop()
return False
result = {}
def process():
data = pop_queue()
while data:
result[data] = task(*data)
pbar.update(1)
data = pop_queue()
threads = []
for i in range(number_of_threads):
threads.append(threading.Thread(target=process))
for thread in threads:
thread.start()
for thread in threads:
thread.join()
return result
def open_json(file):
"""Read a json file and returns its content has a dict
"""
with open(file) as f:
data = json.load(f)
return data
return None