setdefaulttimeout与用Python编写的Windows服务有什么关系?

64jmpszr  于 2023-04-22  发布在  Windows
关注(0)|答案(1)|浏览(98)

我正在熟悉用Python创建Windows服务,我在这里或其他地方找到的所有示例都调用了socket.setdefaulttimeout(60.0)(或类似的东西)。但是这些示例都没有解释 * 为什么 * 这样做......我找不到任何关于这是否与Windows服务有关或与Python中实现它们的方式有关的信息。
我只是想更好地处理我的代码中发生的事情。如果有人能解释为什么这样做/如果需要这样做,我将不胜感激。
作为参考,下面是Python中服务类的(部分)样板实现

import socket
import win32event
from win32serviceutil import ServiceFramework
# some of the usual imports have been left out for the sake of brevity

class ExampleService(ServiceFramework):
    _svc_name_ = 'example_service'
    _svc_display_name_ = 'Example Service'
    _svc_description_ = 'Lorem ipsum dolor sit amet'

    def __init__(self, *args):
        super().__init__(*args)
        socket.setdefaulttimeout(60.0)  # <- this is the line in question
        self.svc_stop_event = win32event.CreateEvent(None, 0, 0, None)

    def SvcStop(self):
        ...

    def SvcDoRun(self):
        ...

***更新:***我已经从我的代码中删除了对socket.setdefaulttimeout()的调用,不出所料,没有任何东西被破坏。美好的时光!

mec1mxoz

mec1mxoz1#

套接字与编写服务无关。但是,套接字可以在服务内部使用。显然,您必须研究服务如何使用套接字,以便理解为什么要设置默认套接字超时。设置超时不会影响服务本身。

相关问题