英文:
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_ = '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):
...
<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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论