Python 全局锁被忽略?
我正在使用Python 全局锁被忽略?,python,locking,Python,Locking,我正在使用SimpleHTTPServer作为代理。我的程序过于简单,如下所示: print_lock = Lock() class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler): def do_GET(self): displayABC() displayDEF() def do_POST(self): displayABC() def displayABC():
SimpleHTTPServer
作为代理。我的程序过于简单,如下所示:
print_lock = Lock()
class Proxy(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_GET(self):
displayABC()
displayDEF()
def do_POST(self):
displayABC()
def displayABC():
print_lock.acquire()
print "A"
print "B"
print "C"
print_lock.release()
def displayDEF():
print_lock.acquire()
print "D"
print "E"
print "F"
print_lock.release()
httpd = SocketServer.ForkingTCPServer(('', 80), Proxy)
httpd.serve_forever()
由于许多请求的传入速度非常快,因此显示
方法在请求之间重叠,即:
A
D # -> Note that they are 'out of order' in the output
E
F
B
C
...
理想情况下,我希望所有印刷品都采用相同的方法,以相互遵循:
A
B # -> Correct order
C
D
E
F
...
您可以在我尝试使用
Lock()
的代码中看到,这没有效果。我搜索了一些类似的问题,但它们都涉及线程。这是您的分叉服务器(嘻嘻,嘻嘻)。因为它是分叉的,所以锁在不同的进程中。multiprocessing.Lock可能会解决问题。