-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpmt1.py
73 lines (64 loc) · 2.01 KB
/
mpmt1.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# mpmt1.py: A stupid simple example of Python threading and multiprocessing.
# By specifying '-m t' (threading) or '-m p' (multiprocessing), you can see
# cpu usage rate are different. In case threading, at most 100%, but
# in case multiprocessing you can consume as much number of cpus available.
#
# License:
# Apache License, Version 2.0
# History:
# * 2021/12/20 v0.1 Initial version
# Author:
# Masanori Itoh <[email protected]>
# TODO:
# * Add HELP messages.
import time
import threading
import multiprocessing
import os
import sys
import getopt
def busy_worker(identity, duration):
print('busy_worker: %d' % (identity))
ts_orig = time.time()
while True:
#time.sleep(0)
ts = time.time()
if (ts - ts_orig > duration):
return
if __name__ == "__main__":
num_context = 4
duration = 5
use_thread = 1 # 1: threading, 0: multiprocessing
try:
opts, args = getopt.getopt(sys.argv[1:], "n:d:m:")
except getopt.GetoptError as err:
print(str(err))
sys.exit(2)
for o, a in opts:
if o == '-n':
num_context = int(a)
elif o == '-d':
duration = int(a)
elif o == '-m':
if a == 't' or a == 'T':
use_thread = 1
elif a == 'p' or a == 'P':
use_thread = 0
print('num_context: %d, duration; %d, mode: %s' %
(num_context, duration, 'threading' if use_thread else 'multiprocessing'))
workers = []
for i in range(0, num_context):
print('creating worker: %d (mode: %s)' % (i, 'thread' if use_thread else 'process'))
if use_thread:
w = threading.Thread(target=busy_worker, args=(i, duration))
w.name = 'busy_worker/%d' % (i)
else:
w = multiprocessing.Process(target=busy_worker, args=(i, duration))
workers.append(w)
w.start()
for w in workers:
w.join()
print('join: %s %s' % (w.name, w))