def run( self ):
# This is the main server loop for the Win32 platform
import win32pipe
import win32file
self.pipeHandle = win32pipe.CreateNamedPipe(
'\\\\.\\pipe\\myapp_requests',
win32pipe.PIPE_ACCESS_DUPLEX,
win32pipe.PIPE_TYPE_BYTE |
win32pipe.PIPE_READMODE_BYTE |
win32pipe.PIPE_WAIT,
1,
4096,
4096,
10000,
None)
if self.pipeHandle == win32file.INVALID_HANDLE_VALUE:
print 'Failed to create named pipe %s!' % self.pipeName
print 'Exiting...'
sys.exit(1)
while True:
# Open file connection
win32pipe.ConnectNamedPipe( self.pipeHandle )
# Run the main message loop until it exits, usually because
# of a loss of communication on the pipe
try:
self.messageLoop()
except ServerKillSignal:
break
# Return the pipes to their disconnected condition and try again
try: win32pipe.DisconnectNamedPipe( self.pipeHandle )
except: pass
win32file.CloseHandle( self.pipeHandle )
print "Exiting server"
丹·海德的回答正是你想要的。在
第#2项“如果第一个脚本尝试发送消息时接收器未运行,则它已启动”,可能无法工作。必须有东西在运行才能接收消息。我建议编写一个demon进程,在启动时启动,并告诉Windows如果它死了就重新启动它。在
我构建了一个使用Windows命名管道的服务器,使用以下密钥代码:
方法
messageLoop()
从管道读取数据,使用win32file.ReadFile()
,直到win32文件.错误被抛出。然后它退出,允许run()重新启动它。在在我的实现中,用户不太可能具有管理员访问权限,因此无法将其作为系统服务启动。相反,我对客户机进行了编码,以检查“\.\pipe\pyccf\u requests”处是否存在管道。如果它不存在,则客户端将启动一个新的服务器进程。在
我会考虑使用^{} 作为消息传递的一部分,听起来你已经在考虑日志类型用例了。在
标准库日志记录工具非常灵活,并且是由配置驱动的,因此您应该能够根据您的需求调整它们。在
这不能处理你问题中自动重启的部分。对于UNIX,您可能只需要使用pid文件和
os.kill(pid, 0)
来检查它是否正在运行,但是我不知道在Windows世界中它们的等价物是什么。在相关问题 更多 >
编程相关推荐