-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpython_multithreading.py
More file actions
57 lines (37 loc) · 1.15 KB
/
Copy pathpython_multithreading.py
File metadata and controls
57 lines (37 loc) · 1.15 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
import time
import threading
import math
import pandas as pd
###等分某个大的dataframe ,使用多个线程去处理
def do_something(df):
for i in df:
print(i)
def process_in_threads(total_df: pd.DataFrame, target, thread_number=20):
length = len(total_df.values)
size = math.ceil(length / thread_number)
print("There are %s records need to update...\n" % length)
start_time = time.time()
threads = []
for i in range(0, thread_number):
start = i * size
end = (i + 1) * size
if i == thread_number - 1 and end != length:
end = length
sub_df = total_df[start: end]
t = threading.Thread(target=target, args=(sub_df,))
threads.append(t)
for t in threads:
t.start()
for t in threads:
t.join()
end_time = time.time()
print('Done, Time cost: %s ' % (end_time - start_time))
def p(thread_index, msg):
print("Thread %s - %s" % (thread_index, msg))
pass
if __name__ == '__main__':
demo_list = []
for i in range(1000):
demo_list.append(i)
df = pd.DataFrame(demo_list)
process_in_threads(df, do_something)