在Windows上安全地杀死python脚本 [英] Safely killing a python script on Windows

查看:68
本文介绍了在Windows上安全地杀死python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个python脚本来定期抓取网站并将结果存储在工作中的.json文件中.该脚本被设置为在无命令行的情况下无限循环运行,因此我可以让它从早上开始,然后整天不受阻碍地运行.

I have created a python script to regularly scrape a website and store the results in a .json file while at work. This script is set to run without the command line on an infinite loop, so I can have it start in the morning and then just run unimpeded throughout the day.

我的问题是在晚上我想杀死它以便可以回家,但是我不想在中间连接或中间写入时造成任何问题.我想知道在Windows 10上处理此类脚本以处理退出行为的正确方法是什么.

My issue is that at night I'm going to want to kill it so that I can go home, but I don't want to cause any issues with killing it mid-connection or mid-write. I was wondering what was the correct way to handle creating, and subsequently destroying, such a script on Windows 10 to handle exit behaviours.

推荐答案

下面是一个示例,该示例使用隐藏窗口来监听taskkill.exe发送的 WM_CLOSE 消息.

Here's an example that uses a hidden window to listen for a WM_CLOSE message sent by taskkill.exe.

demo.py

import win32con
import win32gui

def shutdown_monitor():
    def wndproc(hwnd, msg, wparam, lparam):
        if msg == win32con.WM_CLOSE:
            win32gui.DestroyWindow(hwnd)
            return 0
        elif msg == win32con.WM_DESTROY:
            win32gui.PostQuitMessage(0)
            return 0
        return win32gui.DefWindowProc(hwnd, msg, wparam, lparam)
    wc = win32gui.WNDCLASS()
    wc.lpszClassName = 'SpamMessages'
    wc.lpfnWndProc = wndproc
    win32gui.RegisterClass(wc)
    hwnd = win32gui.CreateWindow('SpamMessages', 'Python Spam App',
                0, 0, 0, 0, 0, 0, 0, 0, None)
    win32gui.PumpMessages()

if __name__ == '__main__':
    import sys
    import time
    import atexit
    import threading

    atexit.register(print, 'PYTHON SPAM APP: SHUTDOWN')

    shutdown_thread = threading.Thread(target=shutdown_monitor)
    shutdown_thread.start()
    shutdown_thread.join()
    time.sleep(1)
    sys.exit(0x2A)

demo.bat

@echo off
setlocal
set killquit=taskkill /fi "windowtitle eq Python Spam App"
set killkill=taskkill /f /fi "windowtitle eq Python Spam App"

echo Quit Test
start /b cmd /c "(timeout 3 >nul) & %killquit%" & demo.py
echo ExitCode=%=ExitCode%
echo.
echo Kill Test
start /b cmd /c "(timeout 3 >nul) & %killkill%" & demo.py
echo ExitCode=%=ExitCode%

输出

Quit Test
SUCCESS: Sent termination signal to the process with PID 5612.
PYTHON SPAM APP: SHUTDOWN
ExitCode=0000002A

Kill Test
SUCCESS: The process with PID 5432 has been terminated.
ExitCode=00000001

Windows 8/10任务管理器不认为此隐藏窗口属于交互式应用程序,也不在其应用程序"列表中列出该进程,因此与taskkill.exe不同,它强制终止该进程而不是发布 WM_CLOSE .可能还有其他方法可以强制任务管理器将流程归类为应用".

The Windows 8/10 Task Manager doesn't consider this hidden window as belonging to an interactive application and doesn't list the process in its "Apps" list, so unlike taskkill.exe it forcefully terminates the process instead of posting WM_CLOSE. There may be some other way to force Task Manager to classify the process as an 'app'.

这篇关于在Windows上安全地杀死python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆