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

huangapple go评论55阅读模式
英文:

What does socket.setdefaulttimeout have to do with Windows services written in Python?

问题

I'm familiarizing myself with creating Windows services in Python, and all of the examples I've found here or elsewhere call socket.setdefaulttimeout(60.0) (or something similar). But none of these examples explain why this is being done...and I can't find any info regarding whether or not this has to do with Windows services or with the way they're implemented in Python.

I'm just trying to get a better handle of what's going on in my code. If anyone can explain why this is done / if it's needed, I'd appreciate the help.

For reference, here's a (partial) boilerplate implementation of a service class in 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):
        ...

Update:
I've removed the call to socket.setdefaulttimeout() from my code, and unsurprisingly nothing was broken. Good times!

英文:

I'm familiarizing myself with creating Windows services in Python, and all of the examples I've found here or elsewhere call socket.setdefaulttimeout(60.0) (or something similar). But none of these examples explain why this is being done...and I can't find any info regarding whether or not this has to do with Windows services or with the way they're implemented in Python.

I'm just trying to get a better handle of what's going on in my code. If anyone can explain why this is done / if it's needed, I'd appreciate the help.

For reference, here's a (partial) boilerplate implementation of a service class in 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_ = &#39;example_service&#39;
    _svc_display_name_ = &#39;Example Service&#39;
    _svc_description_ = &#39;Lorem ipsum dolor sit amet&#39;

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

    def SvcStop(self):
        ...

    def SvcDoRun(self):
        ...

<hr>

Update:
I've removed the call to socket.setdefaulttimeout() from my code, and unsurprisingly nothing was broken. Good times!

答案1

得分: 2

Sockets have nothing to do with writing services. But, sockets can be used inside of services. Obviously, you would have to study how the services are using the sockets in order to understand why a default socket timeout is being set. Setting the timeout won't affect the service itself.

英文:

Sockets have nothing to do with writing services. But, sockets can be used inside of services. Obviously, you would have to study how the services are using the sockets in order to understand why a default socket timeout is being set. Setting the timeout won't affect the service itself.

huangapple
  • 本文由 发表于 2023年4月20日 03:53:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76058342.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定