将 Python 脚本作为 Windows 后台进程运行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32808730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 12:18:28  来源:igfitidea点击:

Running Python Script as a Windows background process

pythonwindowsbackground-process

提问by Vikas D

I have been trying to code a python script which reads data from a serial port. It worked well from the command line but I need it to run as a background process without any command line interface. The script has a while loop which reads the next byte of data from a serial port and simulates a key press accordingly. For the keypresses to be focused on the current window, instead of python command line, I need the script to run as a background process. I have read few answers here but nothing rang a bell for me. Please point me in the right direction.

我一直在尝试编写一个从串行端口读取数据的 python 脚本。它从命令行运行良好,但我需要它作为没有任何命令行界面的后台进程运行。该脚本有一个 while 循环,它从串行端口读取下一个字节的数据并相应地模拟按键操作。为了让按键集中在当前窗口而不是 python 命令行,我需要脚本作为后台进程运行。我在这里读到的答案很少,但没有给我敲响警钟。请指出我正确的方向。

采纳答案by Joseph Farah

On Windows, you can run a Python script in the background using the pythonw.exeexecutable. This will run your program in the background, with no visible process or way to interact with it. You also cannot terminate it without a system monitor. More information on pythonw.exehere: pythonw.exe or python.exe?

在 Windows 上,您可以使用pythonw.exe可执行文件在后台运行 Python 脚本。这将在后台运行您的程序,没有可见的进程或与之交互的方式。如果没有系统监视器,您也无法终止它。有关pythonw.exe此处的更多信息: pythonw.exe 或 python.exe?

Best of luck!

祝你好运!