在 Windows 上,您可以使用pythonw.exe https://docs.python.org/2/using/windows.html#executing-scripts为了将 python 脚本作为后台进程运行:
Python 脚本(扩展名为.py
)将被执行python.exe
默认情况下。该可执行文件打开一个终端,该终端停留在
即使程序使用 GUI 也可以打开。如果您不希望这样
发生这种情况,请使用扩展程序.pyw
这将导致脚本
执行者pythonw.exe
默认情况下(两个可执行文件都位于
Python 安装目录的顶层)。这抑制了
启动时的终端窗口。
例如,
C:\ThanosDodd\Python3.6\pythonw.exe C:\\Python\Scripts\moveDLs.py
为了使你的脚本连续运行,你可以使用sched https://docs.python.org/3/library/sched.html对于事件安排:
sched 模块定义了一个实现通用目的的类
事件调度程序
import sched
import time
event_schedule = sched.scheduler(time.time, time.sleep)
def do_something():
print("Hello, World!")
event_schedule.enter(30, 1, do_something, (sc,))
event_schedule.enter(30, 1, do_something, (s,))
event_schedule.run()
现在,为了终止 Windows 上的后台进程,您只需运行:
taskkill /pid processId /f
Where processId
是您要终止的进程的 ID。