import
multiprocessing
import
time
class
myProcess(multiprocessing.Process):
def
__init__(
self
, name, n):
super
().__init__()
self
.name
=
name
self
.n
=
n
self
.daemon
=
True
def
run(
self
):
while
self
.n:
print
(f
'程序 {self.name} 即将在 {self.n} 秒后停止'
)
self
.n
-
=
1
time.sleep(
0.6
)
exit(
0
)
def
kill(
self
):
super
().kill()
print
(
'========================'
)
print
(f
'程序 {self.name} 已停止'
)
print
(
'========================'
)
if
__name__
=
=
'__main__'
:
pool
=
{}
while
True
:
print
(pool.keys())
name
=
input
(
'输入 q 退出主程序!\n请输入运行程序名:'
)
if
name
in
pool.keys():
if
pool[name].is_alive():
print
(
'****************************'
)
print
(f
'程序 {name} 正在运行中...'
)
print
(
'****************************'
)
else
:
pool.pop(name)
pool.update({name:myProcess(name,
10
)})
pool[name].start()
elif
name
=
=
'q'
:
break
else
:
if
len
(pool)
=
=
1
:
oldName
=
list
(pool.keys())[
0
]
if
pool[oldName].is_alive():
pool[oldName].kill()
pool.pop(oldName)
pool.update({name:myProcess(name,
10
)})
pool[name].start()