利用flask自带的werkzeug模块实现
# 监控脚本
# 首先需要 pip install flask
# pymonitor
.
py
def
run_with_reloader
(
main_func
,
args
=
(
)
,
kwargs
=
None
,
extra_files
=
None
,
interval
=
1
,
reloader_type
=
'auto'
)
:
""
"Run the given function in an independent python interpreter."
""
import
os
import
sys
import
signal
import
threading
from
werkzeug
.
_reloader
import
reloader_loops
reloader
=
reloader_loops
[
reloader_type
]
(
extra_files
,
interval
)
signal
.
signal
(
signal
.
SIGTERM
,
lambda
*
args
:
sys
.
exit
(
0
)
)
try
:
if
os
.
environ
.
get
(
'WERKZEUG_RUN_MAIN'
)
==
'true'
:
t
=
threading
.
Thread
(
target
=
main_func
,
args
=
args
,
kwargs
=
kwargs
)
t
.
setDaemon
(
True
)
t
.
start
(
)
reloader
.
run
(
)
else
:
sys
.
exit
(
reloader
.
restart_with_reloader
(
)
)
except KeyboardInterrupt
:
pass
# 运行脚本
import
time
from
pymonitor
import
run_with_reloader
def
main
(
name
,
age
)
:
while
True
:
print
(
"hello, "
,
name
,
'!'
)
time
.
sleep
(
2
)
print
(
age
)
if
__name__
==
'__main__'
:
run_with_reloader
(
main
,
args
=
(
'man'
,
20
)
)
参考链接: https://fuyunliu.github.io/2018/03/09/python-reloader/