python超时退出进程_长时间运行进程的超时和Windows服务(Python)

将服务用作具有多个线程的控制器。

一个线程(Main)应该同步和排队命令,并使用win32 serviceframework进行通信、注册到系统等。

另一个线程(Worker)应该等待来自队列的命令并执行它们。如果您将任意代码作为单独的进程执行,那么您可以从工作线程中派生这些代码,并在它们完成后简单地读回结果并进行清理。在

这样,当一个stop到达时,您的主线程将把它注册到它队列中的worker,它将唤醒并尝试向其他进程发出退出信号,稍等片刻,然后强制清理或终止它们。在

更新:

下面是一个示例概念,介绍如何使服务始终响应并根据需要运行多长时间。每个工人都可以扔...

import threading

...

class InterruptedException(Exception):

pass

class WorkerThread(threading.Thread):

def __init__(self, controller):

self._controller = controller

self._stop = threading.Event()

super(WorkerThread, self).__init__()

def stop(self):

self._stop.set()

def stopped(self):

return self._stop.isSet()

def run(self):

try:

# Insert the code you want to run as a service here

# rather than do "execfile(.../.../blah)" simply do:

# You can have your code throw InterruptedException if your code needs to exit

# Also check often if self.stopped and then cleanly exit

import your_file

your_file.main()

# if code in another module is not yours or cannot check often if it should stop then use multiprocessing which will spawn separate processes that you can terminate then from here when you need to stop and return

# in that case simply block here on self._stop.wait()

except InterruptedException as exc:

# We are forcefully quitting

pass

except Exception as e:

# Oh oh, did not anticipate this, better report to Windows or log it

finally:

# Close/release any connections, handles, files etc.

# OK, we can stop now

win32event.SetEvent(self._controller)

def __init__(self, args):

win32serviceutil.ServiceFramework.__init__(self, args)

self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)

self.hWaitDone = win32event.CreateEvent(None, 0, 0, None)

def SvcDoRun(self):

import servicemanager

servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,

servicemanager.PYS_SERVICE_STARTED,(self._svc_name_, ''))

worker = WorkerThread(self.hWaitDone)

worker.start()

while True:

# Wait for service stop signal

rc = win32event.WaitForMultipleObjects([self.hWaitStop, self.hWaitDone], win32event.INFINITE)

# Check to see if self.hWaitStop happened as part of Windows Service Management

if rc == 0:

# Stop signal encountered

servicemanager.LogInfoMsg(self._svc_name_ + " - STOPPED!") #For Event Log

break

if rc == 1:

# Wait until worker has fully finished

worker.join()

# Determine from worker state if we need to start again (because run finished)

# Or do whatever

if not worker.need_to_start_again():

break

worker.start()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值