连接时服务器引导(警告:LONG) [英] server bootstrapping upon connection (WARNING: LONG)

查看:61
本文介绍了连接时服务器引导(警告:LONG)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是这种情况:我希望我的服务器在连接时启动。

当第一个连接进来时,服务器没有运行。

客户端意识到这一事实,然后启动服务器并尝试再次连接
。这当然都发生在同一台机器上(仅限本地

连接)。


连接速度可以达到30 + /秒,所以服务器是

线程(使用SocketServer.ThreadingTCPServer)。此外,服务器

初始化分为两个:一个是真实的,冗长的

设置,另一个是尽快开始lisenting。

问题:我需要防止启动服务器的多个副本

。我通过使用文件锁定(fcntl.lockf())来做到这一点。但是,

并非每次代码成功阻止服务器启动时间超过一次。以下是相关代码:


----客户代码链接开始----

来自fooConsts导入*


导入套接字,fcntl,os,sys,time,logging

FOO_CLIENT_DEBUG = 1


POLL_SERVER_INTVL = 0.5


如果FOO_CLIENT_DEBUG:

myPID = os.getpid()

logger = logging.getLogger(''FOO Client'')

hdlr = logging.FileHandler(TMP_PATH +''fooClient.log'')

formatter = logging.Formatter(''%(asctime)s%(message)s'')

hdlr.setFormatter(格式化程序)

logger.addHandler(hdlr)

logger.setLevel(logging.INFO)

>
def log(info):

logger.info(''%d:%s''%(myPID,info))

class FooClient:

def __init __(self,startServer = True):

"""连接到FooServer(如果存在),否则启动它并

连接到它"

self.connected = True

if self .connect():返回

elif不是startServer:

如果FOO_CLIENT_DEBUG:log(''连接失败1'')

self.connected = False

返回


#尝试获取启动服务器的权限;如果我们不能,

别人

#必须这样做 - 尝试重新连接

试试:

如果FOO_CLIENT_DEBUG:log(''尝试正确开始

服务器'')

serverStartLock = open(TMP_PATH +''serverStart.lock'','' w'')

fcntl.lockf(serverStartLock.fileno(),fcntl.LOCK_EX | fcntl.LOCK_NB)

除外:

如果FOO_CLIENT_DEBUG :log(''别人正在做;等待

2'')

而1:

time.sleep(POLL_SERVER_INTVL)

如果self.connect():返回


#安全启动服务器并连接到它

如果FOO_CLIENT_DEBUG:log( ''启动服务器'')

exitCode = os.system(''python -OO"%sfooServer.py"&''%FOO_PATH)

if exitCode == 0:

如果FOO_CLIENT_DEBUG:log(''服务器正在启动;等待

3'')

而1:

time.sleep(POLL_SERVER_INTVL)

if self.connec t():返回

否则:

如果FOO_CLIENT_DEBUG:log(''sever bootstrapping failed'')

self.connected = False

加注无法启动FOOServer


def connect(self):

"""尝试连接到FOOServer从PORT_MIN到

PORT_MAX"""

如果FOO_CLIENT_DEBUG:log(''连接尝试'')

port = PORT_MIN

端口< = PORT_MAX:

试试:

self.socket = socket.socket(socket.AF_INET,

socket.SOCK_STREAM)

self.socket.connect(('''',port))

break

除了:

self.socket.close()

port + = 1


如果端口> PORT_MAX:

如果FOO_CLIENT_DEBUG :log(''连接失败2'')

返回False

如果FOO_CLIENT_DEBUG:log(''连接在端口成功

%d ''%port)

返回True

....


Foo客户端()

----客户端代码链接结束----


从日志中(出现问题时)我甚至看到* AFTER *服务器是

启动和接受的连接(几个连接来了又开始了

愉快),一个连接进来并点击连接失败1

日志行。这不应该发生在FooClient的初始值为

startServer .__ init __()为True。在同样的事件中,FooServer启动了两次,但在日志中没有任何痕迹

的两个启动服务器行,但只有连接失败1,在日志的最后一行




我发现这是一个相当复杂的问题,请求帮助,但我是

真的在智慧结束。随意跳过详细说明,

只是给我一些一般提示和提示。非常感谢你!

Here is the situation: I want my server started up upon connection.
When the first connection comes in, the server is not running. The
client realizes the fact, and then starts up the server and tries to
connect again. This of course all happens on the same machine (local
connection only).

The connections can come in as fast as 30+/sec, so the server is
threaded (using SocketServer.ThreadingTCPServer). Further, the server
initialization is threaded into two: one is to do the real, lengthy
setup, the other is start lisenting ASAP.

The problem: I need to prevent multiple copies of the server being
started. I did this by using file locking (fcntl.lockf()). However,
not every time the code successfully prevented the server being
started up more than one time. Here is the relevant code:

---- CLIENT CODE SNIPPET STARTS ----
from fooConsts import *

import socket,fcntl,os,sys,time,logging

FOO_CLIENT_DEBUG=1

POLL_SERVER_INTVL=0.5

if FOO_CLIENT_DEBUG:
myPID=os.getpid()
logger = logging.getLogger(''FOO Client'')
hdlr = logging.FileHandler(TMP_PATH+''fooClient.log'')
formatter = logging.Formatter(''%(asctime)s %(message)s'')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.INFO)

def log (info):
logger.info(''%d: %s''%(myPID,info))
class FooClient:
def __init__ (self, startServer=True):
"""Connects to FooServer if it exists, otherwise starts it and
connects to it"""
self.connected=True
if self.connect(): return
elif not startServer:
if FOO_CLIENT_DEBUG: log(''connection failed 1'')
self.connected=False
return

# try to obtain the right to start a server; if we can''t,
someone else
# must be doing it - try to reconnect
try:
if FOO_CLIENT_DEBUG: log(''try to get right to start
server'')
serverStartLock=open(TMP_PATH+''serverStart.lock'','' w'')
fcntl.lockf(serverStartLock.fileno(),fcntl.LOCK_EX |fcntl.LOCK_NB)
except:
if FOO_CLIENT_DEBUG: log(''someone else is doing it; wait
2'')
while 1:
time.sleep(POLL_SERVER_INTVL)
if self.connect(): return

# safe to start a server and connect to it
if FOO_CLIENT_DEBUG: log(''start server'')
exitCode=os.system(''python -OO "%sfooServer.py" &''%FOO_PATH)
if exitCode==0:
if FOO_CLIENT_DEBUG: log(''server is being started; wait
3'')
while 1:
time.sleep(POLL_SERVER_INTVL)
if self.connect(): return
else:
if FOO_CLIENT_DEBUG: log(''sever bootstrapping failed'')
self.connected=False
raise "Cannot start FOOServer"

def connect (self):
"""Attempts to connect to FOOServer from PORT_MIN to
PORT_MAX"""
if FOO_CLIENT_DEBUG: log(''connection attempt'')
port=PORT_MIN
while port<=PORT_MAX:
try:
self.socket=socket.socket(socket.AF_INET,
socket.SOCK_STREAM)
self.socket.connect(('''',port))
break
except:
self.socket.close()
port+=1

if port>PORT_MAX:
if FOO_CLIENT_DEBUG: log(''connection failed 2'')
return False
if FOO_CLIENT_DEBUG: log(''connection succeeded at port
%d''%port)
return True
....

FooClient()
---- CLIENT CODE SNIPPET ENDS ----

From the log (when problem occurred) I see even *AFTER* the server was
started and accepted connections (several connections came and went
happily), a connection would come in and hit the "connection failed 1"
log line. This shouldn''t have happened as the default value of
startServer for FooClient.__init__() is True. In the very same
incident, FooServer was started twice, but in the log there''s no trace
of two "start server" lines, but only the "connection failed 1" line
in the very end of the log.

I realize this is a rather complex question to ask for help, but I''m
really at wits end. Feel free to skip the detailed description, and
just throw me some general tips and hints. Thank you VERY MUCH!

推荐答案

fo ************* @ yahoo.com.tw (Fortepianissimo)写道:
fo*************@yahoo.com.tw (Fortepianissimo) writes:
这是情况:我希望我的服务器在连接时启动。
当第一个连接进来时,服务器没有运行。
客户端意识到这一事实,然后启动服务器并尝试再次连接。这当然都发生在同一台机器上(仅限本地连接)。

连接速度可以达到30 + /秒,因此服务器是有线程的(使用SocketServer.ThreadingTCPServer)。此外,服务器的初始化分为两个:一个是做真实的,冗长的设置,另一个是尽快开始lisenting。

问题:我需要预防
启动了多个服务器副本。我通过使用文件锁定(fcntl.lockf())来做到这一点。但是,并非每次代码都成功阻止服务器启动多次。以下是相关代码:


当使用fcntl.lockf时,同一

进程中的不同FooClient实例将能够锁定文件并启动另一台服务器。您可以使用fcntl.flock来防止或者使用一些全局标记。

还要确保通过保留文件来保持文件打开

(即self.serverStartLock = open(...))。

出于调试目的,删除'if self.connect():return''和

我想你会看到更多服务器正在启动。

----客户代码链接开始----
来自fooConsts import *

import socket,fcntl,os,sys,time,logging



POLL_SERVER_INTVL = 0.5

如果FOO_CLIENT_DEBUG:
myPID = os.getpid()
logger = logging.getLogger(''FOO Client'')
hdlr = logging.FileHandler(TMP_PATH +''fooClient.log'')
formatter = logging.Formatter(''%(asctime)s%(message)s'')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.INFO) )

def log(info):
logge r.info(''%d:%s''%(myPID,info))

class FooClient:
def __init __(self,startServer = True):
" ;""连接到FooServer(如果存在),否则启动它并
连接到它"
self.connected = True
如果self.connect():return < elif不是startServer:
如果FOO_CLIENT_DEBUG:log(''连接失败1'')
self.connected = False
返回

#尝试获取启动服务器的权利;如果我们不能,
其他人
#必须这样做 - 尝试重新连接
尝试:
如果FOO_CLIENT_DEBUG:log(''尝试正确开始
server'')
serverStartLock = open(TMP_PATH +''serverStart.lock'',''w'')fcntl.lockf(serverStartLock.fileno(),fcntl.LOCK_EX | fcntl。 LOCK_NB)
除了:
如果FOO_CLIENT_DEBUG:登录(''别人正在做;等待
2'')
1:
time.sleep(POLL_SERVER_INTVL )
如果self.connect():返回

#safe启动服务器并连接到它
如果FOO_CLIENT_DEBUG:log(''start server'')
exitCode = os.system(''python -OO"%sfooServer.py"&''%FOO_PATH)
如果exitCode == 0:
如果FOO_CLIENT_DEBUG:log(''服务器正在开始;等待
3'')
whil e 1:
time.sleep(POLL_SERVER_INTVL)
如果self.connect():返回
否则:
如果FOO_CLIENT_DEBUG:log(''sever bootstrapping failed'')
self.connected = False
raise"无法启动FOOServer"

def connect(self):
"""尝试从PORT_MIN连接到FOOServer
PORT_MAX"""
如果FOO_CLIENT_DEBUG:log(''连接尝试'')
port = PORT_MIN
端口< = PORT_MAX:
尝试:
self.socket = socket.socket(socket.AF_INET,
socket.SOCK_STREAM)
self.socket.connect(('''',port))
break
除了:
self.socket.close()
端口+ = 1
如果端口> PORT_MAX:
如果FOO_CLIENT_DEBUG:log(''connection fa iled 2'')
返回False
如果FOO_CLIENT_DEBUG:log(''连接成功在端口
%d''%port)
返回True
...

FooClient()
----客户端代码链接结束----

从日志中(出现问题时)我甚至看到* AFTER *服务器启动并接受了连接(几个连接来了又快乐地),连接将进入并点击连接失败1
日志行。这不应该是因为FooClient的启动服务器的默认值.__ init __()为True。在同样的


嗯,可能有太多的连接尝试正在等待......

事件,FooServer启动了两次,但是在日志中有''没有任何痕迹两个启动服务器行,但只有连接失败1,在日志的最后。

我意识到这是一个相当复杂的问题,请求帮助,但我真的很有智慧。请随意跳过详细说明,然后给我一些一般的提示和提示。非常感谢!
Here is the situation: I want my server started up upon connection.
When the first connection comes in, the server is not running. The
client realizes the fact, and then starts up the server and tries to
connect again. This of course all happens on the same machine (local
connection only).

The connections can come in as fast as 30+/sec, so the server is
threaded (using SocketServer.ThreadingTCPServer). Further, the server
initialization is threaded into two: one is to do the real, lengthy
setup, the other is start lisenting ASAP.

The problem: I need to prevent multiple copies of the server being
started. I did this by using file locking (fcntl.lockf()). However,
not every time the code successfully prevented the server being
started up more than one time. Here is the relevant code:
When using fcntl.lockf different FooClient instances in the same
process will be able lock the file and start another server. You could
either use fcntl.flock to prevent that or use some global flag.
Also be sure to keep the file open by keeping a reference to it
(i.e. self.serverStartLock=open(...)).
For debugging purposes, remove that ''if self.connect(): return'' and
I think you''ll see much more servers being started.

---- CLIENT CODE SNIPPET STARTS ----
from fooConsts import *

import socket,fcntl,os,sys,time,logging

FOO_CLIENT_DEBUG=1

POLL_SERVER_INTVL=0.5

if FOO_CLIENT_DEBUG:
myPID=os.getpid()
logger = logging.getLogger(''FOO Client'')
hdlr = logging.FileHandler(TMP_PATH+''fooClient.log'')
formatter = logging.Formatter(''%(asctime)s %(message)s'')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.INFO)

def log (info):
logger.info(''%d: %s''%(myPID,info))
class FooClient:
def __init__ (self, startServer=True):
"""Connects to FooServer if it exists, otherwise starts it and
connects to it"""
self.connected=True
if self.connect(): return
elif not startServer:
if FOO_CLIENT_DEBUG: log(''connection failed 1'')
self.connected=False
return

# try to obtain the right to start a server; if we can''t,
someone else
# must be doing it - try to reconnect
try:
if FOO_CLIENT_DEBUG: log(''try to get right to start
server'')
serverStartLock=open(TMP_PATH+''serverStart.lock'','' w'')
fcntl.lockf(serverStartLock.fileno(),fcntl.LOCK_EX |fcntl.LOCK_NB)
except:
if FOO_CLIENT_DEBUG: log(''someone else is doing it; wait
2'')
while 1:
time.sleep(POLL_SERVER_INTVL)
if self.connect(): return

# safe to start a server and connect to it
if FOO_CLIENT_DEBUG: log(''start server'')
exitCode=os.system(''python -OO "%sfooServer.py" &''%FOO_PATH)
if exitCode==0:
if FOO_CLIENT_DEBUG: log(''server is being started; wait
3'')
while 1:
time.sleep(POLL_SERVER_INTVL)
if self.connect(): return
else:
if FOO_CLIENT_DEBUG: log(''sever bootstrapping failed'')
self.connected=False
raise "Cannot start FOOServer"

def connect (self):
"""Attempts to connect to FOOServer from PORT_MIN to
PORT_MAX"""
if FOO_CLIENT_DEBUG: log(''connection attempt'')
port=PORT_MIN
while port<=PORT_MAX:
try:
self.socket=socket.socket(socket.AF_INET,
socket.SOCK_STREAM)
self.socket.connect(('''',port))
break
except:
self.socket.close()
port+=1

if port>PORT_MAX:
if FOO_CLIENT_DEBUG: log(''connection failed 2'')
return False
if FOO_CLIENT_DEBUG: log(''connection succeeded at port
%d''%port)
return True
...

FooClient()
---- CLIENT CODE SNIPPET ENDS ----

From the log (when problem occurred) I see even *AFTER* the server was
started and accepted connections (several connections came and went
happily), a connection would come in and hit the "connection failed 1"
log line. This shouldn''t have happened as the default value of
startServer for FooClient.__init__() is True. In the very same
Well, maybe too many connection attempts are pending...
incident, FooServer was started twice, but in the log there''s no trace
of two "start server" lines, but only the "connection failed 1" line
in the very end of the log.

I realize this is a rather complex question to ask for help, but I''m
really at wits end. Feel free to skip the detailed description, and
just throw me some general tips and hints. Thank you VERY MUCH!




-

脑机器人技术ag

boppstrasse 64。 55118 mainz。德国

fon +49 6131 211639-1。传真+49 6131 211639-2
http://brainbot.com/ mailto: ra**@brainbot.com



--
brainbot technologies ag
boppstrasse 64 . 55118 mainz . germany
fon +49 6131 211639-1 . fax +49 6131 211639-2
http://brainbot.com/ mailto:ra**@brainbot.com


2004-02-10 13:46:37 -0500, ra ** @ brainbot.com 说:
On 2004-02-10 13:46:37 -0500, ra**@brainbot.com said:
fo ************* @ yahoo.com.tw (Fortepianissimo)写道:
fo*************@yahoo.com.tw (Fortepianissimo) writes:
问题:我需要预防
启动了多个服务器副本。我通过使用文件锁定(fcntl.lockf())来做到这一点。但是,并非每次代码都成功阻止服务器启动多次。以下是相关代码:
当使用fcntl.lockf时,在同一个进程中的不同FooClient实例将能够锁定文件并启动另一台服务器。您可以使用fcntl.flock来阻止它或者使用一些全局标志。
The problem: I need to prevent multiple copies of the server being
started. I did this by using file locking (fcntl.lockf()). However,
not every time the code successfully prevented the server being
started up more than one time. Here is the relevant code:
When using fcntl.lockf different FooClient instances in the same
process will be able lock the file and start another server. You could
either use fcntl.flock to prevent that or use some global flag.




嗯...我不知道有什么flock()和lockf()之间的区别,

我也没有从文档中获得太多信息。你能解释一下为什么lockf()不会锁定文件的原因吗?


实际上我写了一个小脚本来测试lockf()是否做了什么它b / b
声称:


---代码开始---

#!/ usr / bin / env python


import os,fcntl,sys

print" *即将打开flock.txt

f = open(''flock.txt'',''w'')

print" *打开文件

fcntl.lockf(f.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)

打印" *获得锁定,在下面输入你的行:"

l = sys.stdin.readline()

f.truncate()

f.write(l)

f.flush()

sys.stdin.readline ()

f.close()


---代码结束---


似乎它锁定文件? (Mac OS X 10.3.2)。

还要确保通过保持文件的引用来保持文件打开
(即self.serverStartLock = open(...))。出于调试目的,删除
如果self.connect():return''和
我认为你会看到更多服务器正在启动。



Hm... I didn''t know there''s a difference between flock() and lockf(),
and I didn''t get much info from the document either. Could you explain
a bit on why lockf() would not lock the file?

Actually I wrote a tiny script just to test if lockf() does what it
claims to do:

--- CODE STARTS ---
#!/usr/bin/env python

import os,fcntl,sys

print "* about to open flock.txt"
f=open(''flock.txt'',''w'')
print "* opened the file"
fcntl.lockf(f.fileno(),fcntl.LOCK_EX|fcntl.LOCK_NB )
print "* obtained the lock, enter your line below:"
l=sys.stdin.readline()
f.truncate()
f.write(l)
f.flush()
sys.stdin.readline()
f.close()

--- CODE ENDS ---

It seems it does lock the file? (Mac OS X 10.3.2).
Also be sure to keep the file open by keeping a reference to it
(i.e. self.serverStartLock=open(...)). For debugging purposes, remove
that ''if self.connect(): return'' and
I think you''ll see much more servers being started.




--- CODE SNIPPET STARTS ---

class FooClient:

def __init __(self,startServer = True):

"""连接到FooServer,如果它存在,否则启动它并

连接到它""

self.connected = True

if self.connect():return

elif not startServer:

if FOO_CLIENT_DEBUG:log(''connection failed 1'')

self.connected = False

返回

...

--- CODE SNIPPET ENDS ---


那么在这种情况下,每个连接都会尝试启动服务器。好的

指向保持对锁文件的引用 - 我会添加它

并看看会发生什么。



--- CODE SNIPPET STARTS ---
class FooClient:
def __init__ (self, startServer=True):
"""Connects to FooServer if it exists, otherwise starts it and
connects to it"""
self.connected=True
if self.connect(): return
elif not startServer:
if FOO_CLIENT_DEBUG: log(''connection failed 1'')
self.connected=False
return
...
--- CODE SNIPPET ENDS ---

Well in that case every connection will try to start a server. Good
point on keeping a reference to the lock file though - I''ll add to it
and see what happens.


从日志中(当出现问题时)我甚至看到* AFTER *服务器启动并接受了连接(几个连接来了又走了
愉快地),连接将进入并点击连接失败1
日志行。这不应该是因为FooClient的启动服务器的默认值.__ init __()为True。在同一个

From the log (when problem occurred) I see even *AFTER* the server was
started and accepted connections (several connections came and went
happily), a connection would come in and hit the "connection failed 1"
log line. This shouldn''t have happened as the default value of
startServer for FooClient.__init__() is True. In the very same



那么,也许有太多的连接尝试正在等待...



Well, maybe too many connection attempts are pending...




我没看到为什么这个应该影响

参数的默认值...如果startServer为True(默认值),该日志行应该从未达到过



谢谢!


Ben



I failed to see why this should affect the default value of the
argument... if startServer is True (default), that log line should have
never been reached.

Thanks!

Ben


Benjamin Han< th ** @ is。 for.spambot>写道:
Benjamin Han <th**@is.for.spambot> writes:
2004-02-10 13:46:37 -0500, ra ** @ brainbot。 com 说:
On 2004-02-10 13:46:37 -0500, ra**@brainbot.com said:
fo * ************@yahoo.com.tw (Fortepianissimo)写道:
fo*************@yahoo.com.tw (Fortepianissimo) writes:
问题:我需要阻止服务器的多个副本
开始了。我通过使用文件锁定(fcntl.lockf())来做到这一点。但是,并非每次代码都成功阻止服务器启动多次。以下是相关代码:当使用fcntl.lockf时,不同的FooClient实例在同一个进程中将能够锁定文件并启动另一台服务器。你可以使用fcntl.flock来阻止它或使用一些全局标志。
The problem: I need to prevent multiple copies of the server being
started. I did this by using file locking (fcntl.lockf()). However,
not every time the code successfully prevented the server being
started up more than one time. Here is the relevant code: When using fcntl.lockf different FooClient instances in the same
process will be able lock the file and start another server. You could
either use fcntl.flock to prevent that or use some global flag.



嗯...我不知道flock之间有什么区别( )和lockf(),
我也没有从文档中获得太多信息。你能解释一下为什么lockf()不会锁定文件吗?



Hm... I didn''t know there''s a difference between flock() and lockf(),
and I didn''t get much info from the document either. Could you explain
a bit on why lockf() would not lock the file?




好​​吧,它可能会在同一个文件中多次锁定文件/>
流程。这就是问题所在:

----

导入fcntl


def getlockfile():

返回打开(''serverStart.lock'',''w'')


def getlock(f):

尝试:

锁定(f.fileno(),fcntl.LOCK_EX | fcntl.LOCK_NB)

除外:

返回False

返回True


def doit():

f1 = getlockfile()

f2 = getlockfile()

print getlock(f1),getlock(f2)

lock = fcntl.lockf

doit()


lock = fcntl.flock

doit()

---


输出为:

真实

真假

实际上我写了一个小小的脚本只是为了测试lockf()是否做了它所声称的内容执行:

---代码开始---
#!/ usr / bin / env python

导入os,fcntl,sys

print" *即将打开flock.txt"
f = open(''flock.txt'',''w'')
print" * op ened文件
fcntl.lockf(f.fileno(),fcntl.LOCK_EX | fcntl.LOCK_NB)
打印*获得锁定,在下面输入你的行:
l = sys.stdin.readline()
f.truncate()
f.write(l)
f.flush()
sys.stdin.readline()
f.close()

---代码结束---

它似乎确实锁定了文件? (Mac OS X 10.3.2)。



Well, it might lock the file multiple times in the same
process. That''s the problem:
----
import fcntl

def getlockfile():
return open(''serverStart.lock'', ''w'')

def getlock(f):
try:
lock(f.fileno(), fcntl.LOCK_EX|fcntl.LOCK_NB)
except:
return False
return True

def doit():
f1 = getlockfile()
f2 = getlockfile()
print getlock(f1), getlock(f2)

lock = fcntl.lockf
doit()

lock = fcntl.flock
doit()
---

Output is:
True True
True False


Actually I wrote a tiny script just to test if lockf() does what it
claims to do:

--- CODE STARTS ---
#!/usr/bin/env python

import os,fcntl,sys

print "* about to open flock.txt"
f=open(''flock.txt'',''w'')
print "* opened the file"
fcntl.lockf(f.fileno(),fcntl.LOCK_EX|fcntl.LOCK_NB )
print "* obtained the lock, enter your line below:"
l=sys.stdin.readline()
f.truncate()
f.write(l)
f.flush()
sys.stdin.readline()
f.close()

--- CODE ENDS ---

It seems it does lock the file? (Mac OS X 10.3.2).

还要确保通过保持文件的引用来保持文件打开
(即self.serverStartLock = open(... ))。出于调试目的,
删除'if self.connect():return''和
我认为你会看到更多服务器正在启动。
---代码链接开始---
类FooClient:
def __init __(self,startServer = True):
"""连接到FooServer(如果存在),否则启动它并连接
to it"
self.connected = True
如果self.connect():返回
elif不是startServer:
如果FOO_CLIENT_DEBUG:log(''连接失败1 '')
self.connected = False
返回
..
--- CODE SNIPPET ENDS ---

那么在每种情况下连接将尝试启动服务器。好的
指向保持对锁文件的引用 - 我会添加它
并看看会发生什么。
Also be sure to keep the file open by keeping a reference to it
(i.e. self.serverStartLock=open(...)). For debugging purposes,
remove that ''if self.connect(): return'' and
I think you''ll see much more servers being started.
--- CODE SNIPPET STARTS ---
class FooClient:
def __init__ (self, startServer=True):
"""Connects to FooServer if it exists, otherwise starts it and
connects to it"""
self.connected=True
if self.connect(): return
elif not startServer:
if FOO_CLIENT_DEBUG: log(''connection failed 1'')
self.connected=False
return
..
--- CODE SNIPPET ENDS ---

Well in that case every connection will try to start a server. Good
point on keeping a reference to the lock file though - I''ll add to it
and see what happens.
从日志中(当问题发生时)我甚至看到* AFTER *服务器启动并接受了连接(几个连接来了又快乐地),一个连接会进来,点击连接失败1
日志行。这不应该是因为FooClient的启动服务器的默认值.__ init __()为True。在同一个
From the log (when problem occurred) I see even *AFTER* the server
was
started and accepted connections (several connections came and went
happily), a connection would come in and hit the "connection failed 1"
log line. This shouldn''t have happened as the default value of
startServer for FooClient.__init__() is True. In the very same


好吧,也许有太多连接尝试待决......


Well, maybe too many connection attempts are pending...



我没看到为什么这会影响<的默认值br />参数...如果startServer为True(默认值),则该日志行应该从未到过。



I failed to see why this should affect the default value of the
argument... if startServer is True (default), that log line should
have never been reached.




嗯,那么我想你将错误的论点传递给了

FooClient .__ init__。我只是想说,即使服务器在那个端口上听取了
,这种连接尝试也可能会失败。

谢谢!



Well, then I suppose you''re passing an false argument to
FooClient.__init__. I just wanted to say, that even if the server is
listening on that port, that connection attempts may fail.

Thanks!

Ben




-

脑机器人技术ag

boppstrasse 64。 55118 mainz。德国

fon +49 6131 211639-1。传真+49 6131 211639-2
http://brainbot.com/ mailto: ra**@brainbot.com



--
brainbot technologies ag
boppstrasse 64 . 55118 mainz . germany
fon +49 6131 211639-1 . fax +49 6131 211639-2
http://brainbot.com/ mailto:ra**@brainbot.com


这篇关于连接时服务器引导(警告:LONG)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆