Skip to content

Commit 2823364

Browse files
author
xuming06
committed
add thread
1 parent 73e5b88 commit 2823364

3 files changed

Lines changed: 67 additions & 0 deletions

File tree

23thread/async.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# -*- coding: utf-8 -*-
2+
# Author: XuMing <[email protected]>
3+
# Brief:
4+
import time
5+
from multiprocessing import Pool
6+
7+
8+
def function(index):
9+
print('Start process: ', index)
10+
time.sleep(3)
11+
print('End process', index)
12+
13+
14+
if __name__ == '__main__':
15+
pool = Pool(processes=3)
16+
for i in range(14):
17+
pool.apply_async(function, (i,))
18+
19+
print("Started processes")
20+
pool.close()
21+
pool.join()
22+
print("Subprocess done.")

23thread/get_thread_result.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# -*- coding: utf-8 -*-
2+
# Author: XuMing <[email protected]>
3+
# Brief:
4+
import time
5+
from multiprocessing import Pool
6+
7+
8+
def function(index):
9+
print('Start process: ', index)
10+
time.sleep(3)
11+
print('End process', index)
12+
return index
13+
14+
15+
if __name__ == '__main__':
16+
pool = Pool(processes=3)
17+
for i in range(5):
18+
result = pool.apply_async(function, (i,))
19+
print(result.get())
20+
print("Started processes")
21+
pool.close()
22+
pool.join()
23+
print("Subprocess done.")

23thread/thread_demo.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# -*- coding: utf-8 -*-
2+
# Author: XuMing <[email protected]>
3+
# Brief:
4+
import multiprocessing
5+
import time
6+
7+
8+
def process(num):
9+
time.sleep(num)
10+
print('Process:', num)
11+
12+
13+
if __name__ == '__main__':
14+
for i in range(5):
15+
p = multiprocessing.Process(target=process, args=(i,))
16+
p.start()
17+
18+
print('CPU number:' + str(multiprocessing.cpu_count()))
19+
for p in multiprocessing.active_children():
20+
print('Child process name: ' + p.name + ' id: ' + str(p.pid))
21+
22+
print('Process Ended')

0 commit comments

Comments
 (0)