星期三, 八月 08, 2007

python socket buffers and blocking

前面 python socket write/read only 讨论了使用服务器端只写、客户端只读的方式来进行 mirrord/fs_mirror 之间的同步,那么服务器端如下:
...
fconn = conn.makefile('w', 0)
...
while True:
try:
try:
self.wmCond.acquire()
while self.manager.serial == serial: self.wmCond.wait()
sn = serial
serial = self.manager.serial
finally:
self.wmCond.release()
while sn < serial:
action, target = self.manager.wmlog[sn]
# #1:
fconn.write("%s:%s\n" % (action, target)) # OR:
# conn.sendall("%s:%s\n" % (action, target))
if debug:
print "after writing"
sn += 1
self.manager.svPool[self] = sn
客户端现在主要用在测试中,是这样的:
def __check_protocol(self, conn, fconn, md, expect, incr, func, *args):
svCurrent = md.Status.svCurrent()
sn = md.manager.serial
if mirrord.debug: print "sn:", sn
func(*args)
nloop = md.Status.nloop()
while md.Status.nloop() <= nloop + 1: time.sleep(1)
# Before the wmlog being read, the server should reserve it for the client
self.failUnlessEqual(md.manager.svPool[svCurrent], sn
result = []
for i in range(incr):
line = fconn.readline()[:-1]
result.append(line)
result.sort()
expect.sort()
self.failUnlessEqual(result, expect)
nloop = md.Status.nloop()
while md.Status.nloop() <= nloop + 1: time.sleep(1)
# After the wmlog being transported, the server should delete the overdue records
self.failUnlessEqual(md.manager.svPool[svCurrent], sn + incr)
运行测试时,会在上面的
self.failUnlessEqual(md.manager.svPool[svCurrent], sn
处报错,实际是 md.manager.svPool[svCurrent] > sn!

从输出来分析原因,可以发现在服务器端代码的 #1 处并没有被阻塞!也就是说,fconn.write() 或 conn.sendall() 并不会等待客户端收到数据才返回。实际上,socket 和文件操作一样,使用缓冲 buffers 方法,只有数据被 write() 或 sendall() 到缓冲区以后就会立即返回。

这里虽然在前面使用了 fconn = conn.makefile('w', 0),也就是不使用缓冲,按道理来说,应该是要等待数据成功发送才返回的,但因为这个 fconn 只是一个文件对象,使用的是自己的缓冲,它在底层调用 socket 对象的 sendall() 方法,而这个 sendall() 方法在 Python 中一定会隐含使用 buffers,所以这里还是会立即返回。


《Python 网络编程基础》

对于很多操作系统来说,有时候在网络上发送的数据的调用会在远程服务器确保已经收到信息之前返回,因此,很有可能一个来自对 sendall() 成功调用返回的数据,实际上永远都没有被收到。


或者不完整或顺序不对?

总之,recv() 在缓冲是空的时候阻塞,而 sendall() 等应该是在 buffers 满的时候阻塞!

因此,上面的测试代码,在 func(*args) 被调用之后(通常是 os.mkdir,open('...', 'w') 或 shutil.move 之类的),导致文件系统变化,这样服务器端 fconn.write() 之后立刻返回,则该服务线程的 serial 必然增加,而不会等到下面 line = fconn.readline() 的时候,所以比较 serial 的测试会抛出错误。

所以除非使用一开始那种客户端发送确认回执的方法,否则在这里不能进行这种比较(或者也没必要进行这种比较?)。


但这是不是也就意味着在服务器和客户端之间实际上缺乏一种校准机制?
例如服务器端有一个 serial 为 11138 的 wmlog 记录,但客户端可能并没有收到,一种情况是客户端丢掉了这个记录,另一种情况是客户端不得不进行一次完全的初始化操作?

那么是否使用回执的方法就是正确的校准机制呢?

或者我应该向客户端传递 serial 号?

python socket write/read only

最初服务器端的代码是这样的:
while True:
try:
......
# When modified, inform the client first:
conn.sendall("NEXT")
data = conn.recv(1024)
if data != "OK":
raise ServerExc("Sync failed")
sending = []
while sn < serial:
action, target = self.manager.wmlog[sn]
sending.append("%s:%s\n" % (action, target))
if debug:
print "after writing"
sn += 1
self.manager.svPool[self] = sn
# Assign must be an atomic operation, but dict[k] and getattr?
# But I think it is not necessary to Lock at here.
fconn.writelines(sending)
fconn.write("EOF\n")
fconn.flush()
客户端是这样的(同样在一个循环中):
data = conn.recv(1024)
self.failUnlessEqual(data, "NEXT")
conn.sendall("OK")
result = []
for i in range(incr):
line = fconn.readline()[:-1]
if line == 'EOF': break
result.append(line)
这时候,相当于每次服务器端要发送数据之前,都会先通知客户端,并等待客户端的一个回执。这时我考虑,因为两边都在循环,所以我只需要在服务器端不断的写入,在客户端不断的读取就可以了,因为从这里直到线程退出都不会在发送别的类型的数据。

那么就写成这样:
while True:
try:
......
while sn < serial:
action, target = self.manager.wmlog[sn]
fconn.write("%s:%s\n" % (action, target)) # OR:
# conn.sendall("%s:%s\n" % (action, target))
if debug:
print "after writing"
sn += 1
self.manager.svPool[self] = sn
# Assign must be an atomic operation, but dict[k] and getattr?
# But I think it is not necessary to Lock at here.
客户端:
result = []
for i in range(incr):
line = fconn.readline()[:-1]
result.append(line)
但这时就有一个问题,就是之前采用回执办法的时候,客户端退出时服务器端的 conn.recv() 总会得到空,因此可以通过判断来决定是否要关闭这个 socket(但如果是异常终止呢?)。

而现在服务器端不再做判断,如果客户端退出或异常终止,服务器端会出现什么情况呢?使用一个原型来看看:
#!/usr/bin/env python
# -*- encoding: utf-8 -*-

"""
Prototype: server write only, client read only,
if client terminated, what about the server?
This is the server side: write only

Author: Roc Zhou
Date: 2007-08-08
Email: chowroc.z@gmail.com
"""

import sys
import time
import socket
import traceback
import threading

def server(conn):
name = threading.currentThread().getName()
print "Client server: %s" % name
# print "Got connection from: ", conn.getpeername()
conn.sendall("START")
data = conn.recv(1024)
if data != "OK": raise "FAILED"
fconn = conn.makefile('w', 0)
i = 0
while True:
# conn.sendall("message %d" % i)
fconn.write("message %d\n" % i)
i += 1
time.sleep(5)
print "Working thread of %s ending ..." % name
conn.close()

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(('localhost', 2123))
print "Waiting for connections ..."
sock.listen(5)

while True:
try:
conn, addr = sock.accept()

print "Got connection from:", addr
that = threading.Thread(target=server, name='server', args=(conn,))
that.setDaemon(1)
that.start()
except KeyboardInterrupt:
raise
except:
print >> sys.stderr, "CATCH ALL:"
traceback.print_exc()
continue
客户端:
#!/usr/bin/env python
# -*- encoding: utf-8 -*-

"""
Prototype: server write only, client read only,
if client terminated, what about the server?
This is the client side: read only

Author: Roc Zhou
Date: 2007-08-08
Email: chowroc.z@gmail.com
"""

import socket

host = 'localhost'
port = 2123

print "Creating socket ..."
conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print "Connecting ..."
conn.connect((host, port))
print "Done."

data = conn.recv(1024)
if data != "START": raise "FAILED"
conn.sendall("OK")
fconn = conn.makefile('r', 0)

while True:
try:
message = fconn.readline()[:-1]
print message
except KeyboardInterrupt:
raise
运行及其输出

客户端:
sh$ python fs_mirror_10_read_only.py
Creating socket ...
Connecting ...
Done.
message 0
message 1
message 2
message 3
message 4
message 5
message 6
Traceback (most recent call last):
File "fs_mirror_10_read_only.py", line 32, in
message = fconn.readline()[:-1]
File "/usr/lib/python2.5/socket.py", line 331, in readline
data = recv(1)
KeyboardInterrupt


服务器端:
sh# python mirrord_10_write_only.py
Waiting for connections ...
Got connection from: ('127.0.0.1', 38618)
Client server: server
Exception in thread server:
Traceback (most recent call last):
File "/usr/lib/python2.5/threading.py", line 460, in __bootstrap
self.run()
File "/usr/lib/python2.5/threading.py", line 440, in run
self.__target(*self.__args, **self.__kwargs)
File "mirrord_10_write_only.py", line 31, in server
fconn.write("message %d\n" % i)
File "/usr/lib/python2.5/socket.py", line 262, in write
self.flush()
File "/usr/lib/python2.5/socket.py", line 249, in flush
self._sock.sendall(buffer)
error: (32, 'Broken pipe')
或者当不使用 time.sleep(5) 时,服务器端的出错信息如下:
Waiting for connections ...
Got connection from: ('127.0.0.1', 56912)
Client server: server
Exception in thread server:
Traceback (most recent call last):
File "/usr/lib/python2.5/threading.py", line 460, in __bootstrap
self.run()
File "/usr/lib/python2.5/threading.py", line 440, in run
self.__target(*self.__args, **self.__kwargs)
File "mirrord_10_write_only.py", line 30, in server
fconn.write("message %d\n" % i)
File "/usr/lib/python2.5/socket.py", line 262, in write
self.flush()
File "/usr/lib/python2.5/socket.py", line 249, in flush
self._sock.sendall(buffer)
error: (104, 'Connection reset by peer')
总之,会抛出一个 socket.error 的异常,可以通过捕捉这个异常来退出。

但如果使用一边只读,一边只写的情况,就必须要考虑到 socket 对于缓冲(buffers)的使用及其与阻塞(blocking)之间的关系了!

现在的一个问题就是,到底采用那种方式更好呢?理由何在?

星期一, 八月 06, 2007

python socket file object and shutdown()

今天写 mirrord.py 的服务器端和协议的测试代码,在使用 socket 的文件类对象的时候有问题。工作部分的代码如下:
try:
conn = self.conn
fconn = conn.makefile('w', 0)
data = conn.recv(1024)
if data != "INIT SYNC":
raise ServerExc("Invalid init sync command")
try:
self.wmLock.acquire()
# Copy the content from the manager queue at first:
wdirs = self.manager.wdirs.keys()
files = self.manager.files.keys()
serial = self.manager.serial
# And then register itself to the svPool to inform the scheduler
reserving the manager.wmlog for it:
self.manager.svPool[self] = serial
finally:
self.wmLock.release()
conn.sendall("OK")
data = conn.recv(1024)
if data != "INIT SYNC DIRS":
raise ServerExc("Invalid init sync dirs command")
if debug:
print "server wdirs send", wdirs
# #1:
# fconn = conn.makefile('w', 0)
# fconn.writelines("%s\n" % fn for fn in wdirs)
# fconn.close()
# #2:
fconn.writelines("%s\n" % fn for fn in wdirs)
conn.shutdown(1)
# #3:
# fconn = conn.makefile('w', 0)
# for line in wdirs:
# # conn.sendall("%s\n" % line)
# fconn.write("%s\n" % line)
# # fconn.write("^D")
# fconn.write("")
# fconn.close()
# #4:
# for line in wdirs:
# conn.sendall("%s\n" % line)
测试代码实际上就是一个客户端,代码如下:
try:
conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
conn.connect(('localhost', 2123))
fconn = conn.makefile('r', 0)
conn.sendall("START")
data = conn.recv(1024)
conn.sendall("INIT SYNC")
data = conn.recv(1024)
svCurrent = md.Status.svCurrent()
conn.sendall("INIT SYNC DIRS")
# #1:
result_wdirs = [S.strip() for S in fconn.readlines()]
# #2:
# result_wdirs = []
# for line in fconn:
# line = line.strip()
# print line
# result_wdirs.append(line)
# #3:
# data = conn.recv(2048)
# print "DEBUG", data
# #4:
# while 1:
# data = fconn.readline()
# print data
# if not data: break
if mirrord.debug:
print "client wdirs", result_wdirs
expect_wdirs = snap_wdirs
result_wdirs.sort()
expect_wdirs.sort()
self.failUnlessEqual(result_wdirs, expect_wdirs)
conn.sendall("INVALID INIT SYNC FILES CMD")
self.failIf(conn.recv(1024))
self.failUnlessEqual(svCurrent.Status.stage(), mirrord.STAGE_ABORTED)
self.failIf(svCurrent.that.isAlive())
nloop = md.Status.nloop()
while md.Status.nloop() < nloop + 1: time.sleep(1)
self.failIf(md.manager.svPool.has_key(svCurrent))
except socket.error, socex:
self.fail("Communication failed: %s" % socex)
一开始的时候,始终出现一个问题,就是测试代码在运行到上面的片段的时候挂起。按 Ctrl-C 退出,抛出的异常信息如下:
Thread SERVER ABORTED
loop conitnue at 1186393237.81
Thread SERVER ABORTED
loop conitnue at 1186393238.82
loop conitnue at 1186393239.82
server wdirs send ['/root/ulfs/cutils/trunk/testdata/mirrord/tin/tdir1', '/root/ulfs/cutils/trunk/testdata/mirrord/tin']
server wdirs sent ['/root/ulfs/cutils/trunk/testdata/mirrord/tin/tdir1', '/root/ulfs/cutils/trunk/testdata/mirrord/tin']
loop conitnue at 1186393240.83
loop conitnue at 1186393241.83
loop conitnue at 1186393242.84
loop conitnue at 1186393243.84
loop conitnue at 1186393244.85
loop conitnue at 1186393245.85
loop conitnue at 1186393246.85
loop conitnue at 1186393247.86
loop conitnue at 1186393248.86
DELETE FILE: /root/ulfs/cutils/trunk/testdata/mirrord/tin/tfile1
Traceback (most recent call last):
File "mirrord_ut.py", line 1142, in
runner.run(suite)
File "/usr/lib/python2.5/unittest.py", line 705, in run
test(result)
File "/usr/lib/python2.5/unittest.py", line 437, in __call__
return self.run(*args, **kwds)
File "/usr/lib/python2.5/unittest.py", line 433, in run
test(result)
File "/usr/lib/python2.5/unittest.py", line 281, in __call__
return self.run(*args, **kwds)
File "/usr/lib/python2.5/unittest.py", line 260, in run
testMethod()
File "mirrord_ut.py", line 1047, in test_protocol
data = fconn.readline()
File "/usr/lib/python2.5/socket.py", line 331, in readline
data = recv(1)
KeyboardInterrupt
每次都是这样,不论在客户端使用怎样的读取方法,例如在上面的 #1 到 #4 等集中方法。实际上是因为被阻塞在 socket 的 recv(1) 上了。使用客户端代码的 #2,#3 或 #4 会打印出读取的数据,基本上是这样的:
Thread SERVER ABORTED
loop conitnue at 1186393237.81
Thread SERVER ABORTED
loop conitnue at 1186393238.82
loop conitnue at 1186393239.82
server wdirs send ['/root/ulfs/cutils/trunk/testdata/mirrord/tin/tdir1', '/root/ulfs/cutils/trunk/testdata/mirrord/tin']
server wdirs sent ['/root/ulfs/cutils/trunk/testdata/mirrord/tin/tdir1', '/root/ulfs/cutils/trunk/testdata/mirrord/tin']
/root/ulfs/cutils/trunk/testdata/mirrord/tin/tdir1

/root/ulfs/cutils/trunk/testdata/mirrord/tin


loop conitnue at 1186393240.83
loop conitnue at 1186393241.83
loop conitnue at 1186393242.84
loop conitnue at 1186393243.84
loop conitnue at 1186393244.85
loop conitnue at 1186393245.85
loop conitnue at 1186393246.85
loop conitnue at 1186393247.86
loop conitnue at 1186393248.86
DELETE FILE: /root/ulfs/cutils/trunk/testdata/mirrord/tin/tfile1
Traceback (most recent call last):
File "mirrord_ut.py", line 1142, in
runner.run(suite)
File "/usr/lib/python2.5/unittest.py", line 705, in run
test(result)
File "/usr/lib/python2.5/unittest.py", line 437, in __call__
return self.run(*args, **kwds)
File "/usr/lib/python2.5/unittest.py", line 433, in run
test(result)
File "/usr/lib/python2.5/unittest.py", line 281, in __call__
return self.run(*args, **kwds)
File "/usr/lib/python2.5/unittest.py", line 260, in run
testMethod()
File "mirrord_ut.py", line 1047, in test_protocol
data = fconn.readline()
File "/usr/lib/python2.5/socket.py", line 331, in readline
data = recv(1)
KeyboardInterrupt
进一步验证了被阻塞在了发送的数据结束的地方!

那么是不是和 EOF 有关了,是不是因为没有发送文件结束标志呢?但问题是我不知道如何向一个文件对象写入 EOF,尝试了 "^D",也尝试了空字符串,都没有用;后来我想是不是必须关闭文件对象才会写一个 EOF,但在上面的尝试中也不行;使用 conn.sendall() 也不行,仍然被阻塞,除非客户端使用 conn.recv(),但读到的数据不完整(参看上面服务器端工作代码 #1, #3 和 #4):
loop conitnue at 1186395856.35
Thread SERVER ABORTED
Thread SERVER ABORTED
loop conitnue at 1186395857.36
server wdirs send ['/root/ulfs/cutils/trunk/testdata/mirrord/tin/tdir1', '/root/ulfs/cutils/trunk/testdata/mirrord/tin']
DEBUG /root/ulfs/cutils/trunk/testdata/mirrord/tin/tdir1

server wdirs sent ['/root/ulfs/cutils/trunk/testdata/mirrord/tin/tdir1', '/root/ulfs/cutils/trunk/testdata/mirrord/tin']
...
最终,在服务器端使用了 #2 处的代码,调用一个 conn.shutdown(1),这样客户端才终于不再阻塞!

这是从《Python 网络编程基础》上来的:
对于很多操作系统来说,有时候在网络上发送的数据的调用会在远程服务器确保已经收到信息之前返回,因此,很有可能一个来自对 sendall() 成功调用返回的数据,实际上永远都没有被收到。

一旦结束写操作,应该立刻调用 shutdown() 函数,这样会强制清除缓存里面的内容,同时如果有任何问题就会产生一个异常。

...使用 shutdown() 确保服务器完全收到请求...

...请牢记,数据只有在您调用了 shudown() 函数之后才能确保被发送...


但是从 Python lib 里面的内容来看,似乎又不是这个意思,似乎后续写操作都会受影响(上面调用的是 shutdown(1))?是不是这样呢?还得再写程序再看。

实际上,使用 shutdown(1) 会导致后续的写操作都无效,当然 shutdown(1) 会在当前写缓冲区里面的数据都成功写入后才返回。在《Python 网络编程基础》中讲到的半开发 socket 就使用这种方法,当在 fork 或多线程时,使用这种方法防止多个进程/线程之间的数据读写紊乱很有用,但在这里,如果 shutdown(1) 之后不能再写数据,后面的操作全都不能进行了,这显然不是我想要的。

验证一下。服务器端的代码如下:
# #1:
fconn.writelines("%s\n" % fn for fn in wdirs)
fconn.write("EOF\n")
# fconn.flush()
conn.shutdown(1)
fconn.write("EOF...\n")
客户端代码:
while 1:
data = fconn.readline()[:-1]
if not data: break
# if data == 'EOF': break
result_wdirs.append(data)
print "client readline:", data
if mirrord.debug:
print "client wdirs", result_wdirs
输出:
 server wdirs send ['/home/roc/ulfs/cutils/trunk/testdata/mirrord/tin', '/home/roc/ulfs/cutils/trunk/testdata/mirrord/tin/tdir1']
client readline: /home/roc/ulfs/cutils/trunk/testdata/mirrord/tin
client readline: /home/roc/ulfs/cutils/trunk/testdata/mirrord/tin/tdir1
client readline: EOF
client wdirs ['/home/roc/ulfs/cutils/trunk/testdata/mirrord/tin', '/home/roc/ulfs/cutils/trunk/testdata/mirrord/tin/tdir1', 'EOF']
F
可以看出,第二个 EOF... 没有被打印出来,因为此时 conn.shutdown(1) 已经执行了,后面再 fconn.write() 已经不起作用──但为什么没有抛出 socket.error 异常???但后面的
if debug:
print "server wdirs sent", wdirs
倒也确实没有执行!似乎也被阻塞了!!!即便在客户端再加上读取的语句:
result_wdirs = []
while 1:
data = fconn.readline()[:-1]
# if not data: break
if data == 'EOF': break
result_wdirs.append(data)
print "client readline:", data
data = conn.recv(1024)
print data
也一样,data 没有被读取,最终的输出结果为:
loop conitnue at 1186411462.36
Thread SERVER ABORTED
Thread SERVER ABORTED
loop conitnue at 1186411463.41
server wdirs send ['/home/roc/ulfs/cutils/trunk/testdata/mirrord/tin', '/home/roc/ulfs/cutils/trunk/testdata/mirrord/tin/tdir1']
client readline: /home/roc/ulfs/cutils/trunk/testdata/mirrord/tin
client readline: /home/roc/ulfs/cutils/trunk/testdata/mirrord/tin/tdir1
after shutdown(1)
client wdirs ['/home/roc/ulfs/cutils/trunk/testdata/mirrord/tin', '/home/roc/ulfs/cutils/trunk/testdata/mirrord/tin/tdir1']
F
======================================================================
FAIL: testing server and the protocol for communication ...
----------------------------------------------------------------------
Traceback (most recent call last):
File "mirrord_ut.py", line 1069, in test_protocol
self.failUnlessEqual(svCurrent.Status.stage(), mirrord.STAGE_ABORTED)
AssertionError: 1 != 127

----------------------------------------------------------------------
Ran 1 test in 2.241s

FAILED (failures=1)
stage == 1 说明在运行状态,只可能是被阻塞。

但究竟为什么被阻塞而不是抛出 socket.error 的异常呢?

先撇开这个问题,分析一下客户端 readline() 被阻塞的原因。对于循环调用 readline(readlines 就是循环调用 readline),通过分析 socket.py 可以看到,它调用 recv(1),除非得到 "\n" 或 EOF,否则不会终止,但对于 socket 来说,实际上除非关闭这个 socket,才会返回一个 EOF,而因为下面还有数据要读写,所以这里不可能关闭 conn,因此客户端的 readline/readlines 以为下面还有数据要读取,故阻塞等待。

调用 shutdown(1) 从上面的分析来看也不可行。那么只能通过对协议的设计来解决这个问题!服务器端可以在 writelines 后再追加一行 “EOF\n"(注意这里是字符串”EOF“而不是前面的 EOF 标识),客户端在读取到 EOF 字符串后知道传输已经结束了,因此退出循环。这里不存在有名位 EOF 的正常串的问题,因为传送的都是绝对路径的文件路径名,因此必然是以”/“开头的,因此决不会有冲突!

注意,如果在前面在 shutdown(1) 之前忘记增加 fconn.write("EOF\n") 这一行,将导致这样的输出结果:
......
client readline:
client readline:
client readline:
client readline:
client readline:
client readline:
client readline:
client readline:
client readline:
client readline:
client readline:
client readline:
client r
Traceback (most recent call last):
File "mirrord_ut.py", line 1155, in ?
runner.run(suite)
File "/usr/lib/python2.4/unittest.py", line 696, in run
test(result)
File "/usr/lib/python2.4/unittest.py", line 428, in __call__
return self.run(*args, **kwds)
File "/usr/lib/python2.4/unittest.py", line 424, in run
test(result)
File "/usr/lib/python2.4/unittest.py", line 281, in __call__
return self.run(*args, **kwds)
File "/usr/lib/python2.4/unittest.py", line 260, in run
testMethod()
File "mirrord_ut.py", line 1058, in test_protocol
print "client readline:", data
KeyboardInterrupt
除非使用的是 if not data: break 作为判断条件,而不是 if data == 'EOF': break。

这意味着在 socket 的使用中,fconn.readlines() 和 for line in fconn 这样的语法都要注意,一般情况下只能用:
while 1:
line = fconn.readline()[:-1]
if line == 'EOF': break
...
这样的方法!

星期三, 八月 01, 2007

python thread setDaemon

运行一个单元测试程序,结果抛出异常:
roc@crablfs:~/ulfs/cutils/trunk/lib$ python mirrord_ut.py

testing boot init ...
.
testing monitoring ...
Add a dir to watch: /home/roc/ulfs/cutils/trunk/testdata/mirrord/tin
Add a file to record: /home/roc/ulfs/cutils/trunk/testdata/mirrord/tin/tfile1
Add a dir to watch: /home/roc/ulfs/cutils/trunk/testdata/mirrord/tin/tdir1
Add a file to record: /home/roc/ulfs/cutils/trunk/testdata/mirrord/tin/tdir1/tfile2
boot init finished
run schedule finished
loop conitnue at 1185979072.2
Staring socket failed: ('', 2123), (98, 'Address already in use')
Exception in thread schedule:
Traceback (most recent call last):
File "/usr/lib/python2.4/threading.py", line 442, in __bootstrap
self.run()
File "/usr/lib/python2.4/threading.py", line 422, in run
self.__target(*self.__args, **self.__kwargs)
File "/home/roc/ulfs/cutils/trunk/lib/mirrord.py", line 426, in _run_schedule
raise MirrordExc(strerr)
MirrordExc:

......

F
======================================================================
FAIL: test_Monitor (__main__.TestMirrord)
----------------------------------------------------------------------
Traceback (most recent call last):
File "mirrord_ut.py", line 181, in test_Monitor
self.failUnless(md.manager.wdirs.has_key(wd))
AssertionError

----------------------------------------------------------------------
Ran 2 tests in 72.394s

FAILED (failures=1)
测试代码是通过产生一个线程去运行工作代码的,这样可以同步监控工作代码中的变量。测试代码是这样的:
    def test_Monitor(self):
print "\ntesting monitoring ..."
try:
mirrord.testing = 1
testdir = self.testdir

# self.setUp()
failed = self.__build_2()
self.failIf(failed)
md = mirrord.Mirrord(identities=['test2'], datadir="/tmp/mirrord")
# Invalid Server should not can boot the run()
self.failUnlessRaises(mirrord.MirrordExc, md.run, address=('localhost', 2123), Server=1)
# The infinite loop should be terminated correctly when request:
mdt = threading.Thread(target=md.run, name="mirrord", args=(('', 2123),))
mdt.setDaemon(1)
mdt.start()
while md.Status.stage() < 2: time.sleep(1)
md.stop()
nloop = md.Status.nloop()
while md.Status.nloop() == nloop: time.sleep(1)
self.failUnlessEqual(md.Terminate, True)
self.failIf(mdt.isAlive())
self.tearDown()

mirrord.debug = 1
# Test inotify functionality:
self.setUp()
self.__build_1()
snap_wdirs = [testdir, "%s/tin/" % testdir, "%s/tin/tdir1" % testdir]
snap_files = ["%s/tin/tfile1" % testdir, "%s/tin/tdir1/tfile2" % testdir]
md = mirrord.Mirrord(identities=['test1'], datadir="/tmp/mirrord")
mdt = threading.Thread(target=md.run, name="mirrord", args=(('', 2123),))
mdt.setDaemon(1)
mdt.start()
while md.Status.stage() < 2: time.sleep(1)
在测试中,前后两次运行了 md.run,而 md.run(Mirrord.run) 会再产生一个线程调用 socket 绑定到 2123 号端口并监听请求,这里报出'Address already in use'的错误,说明前一个调用产生的 socket 线程没有退出,但是 self.failIf(mdt.isAlive()) 的测试已经通过,也就是说 md.run 这个线程已经退出,为什么 socket 线程仍然在运行呢?

这是否和线程调度中的 setDaemon() 有关呢?在工作代码中原来是这样写的:
def _schedule_thread(self, Server, address):
that = threading.Thread(
target=self._run_schedule, name='schedule', args=(Server, address))
that.setDaemon(1)
that.start()
# that.join()

class Mirrord:
...
def _run_schedule(self, Server, address):
"""
This method runs in the child process/thread
to bind to a port for remote clients' requests
"""
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(address)
sock.listen(5)
except socket.error, socex:
strerr = _("Staring socket failed: %s, %s" % (address, socex))
syslog.syslog(syslog.LOG_ERR, strerr)
if debug: print >> sys.stderr, strerr
raise MirrordExc(strerr)

while not self.Terminate:
try:
conn, addr = sock.accept()
# TODO: Add authentication code here:
output = _("Got connection from:", addr)
syslog.syslog(syslog.LOG_INFO, output)
serv = Server(self.manager, self.wmLock, conn, addr)
serv.start()
# After the sync, create a child process or thread to serve the client
except socket.error, socex:
strerr = _("Get connections error: %s" % socex)
syslog.syslog(syslog.LOG_ERR, strerr)
except:
strerr = traceback.format_exc()
syslog.syslog(syslog.LOG_ERR, strerr)

def run(self, address, Server=ServerThread):
...
self._schedule(Server, address)
...

while not self.Terminate:
try:
if debug:
output = _("loop conitnue at %s" % time.time())
print output
syslog.syslog(syslog.LOG_DEBUG, output)
if self.notifier.check_events():
self.notifier.read_events()
try:
self.wmLock.acquire()
self.notifier.process_events()
# Process the dirs/files moved to the to location unmonitored:
# the question is: will IN_MOVED_FROM and IN_MOVED_TO occur in one polling?
self.processor.clean_cookies()
# TODO: Clean up the overdue manager.wmlog here
temp = self.manager.progress.values()
if temp: temp.sort(); min = temp[0]
else: min = self.manager.serial
for sn in self.manager.wmlog.keys():
if sn < min: self.manager.wmlog.pop(sn)
finally:
self.wmLock.release()
# self.Status.nloop += 1
self.Status.nloop = self.Status.nloop() + 1
except:
strerr = traceback.format_exc()
syslog.syslog(syslog.LOG_ERR, strerr)
if debug: print strerr
break
self.notifier.stop()

def stop(self):
self.Terminate = True
# Or threading Event()?
如果按照字面意思来理解,则如果将子线程设置为 Daemonic,那么在产生它的主线程退出后,它仍将继续运行,虽然在这里设置了 self.Terminate,但因为在 conn, addr = sock.accept() 的时候,线程被阻塞了,而测试代码中并没有产生连接请求,所以该子线程无法检测到 self.Terminate 的变化。

但实际上,即使使用 setDaemon(),除非主线程(即整个进程)退出──这里就是测试代码,停止 md.run 也不会导致 schedule 线程退出。如果设置成 daemonic,则当 main thread 退出的时候,schedule thread 反而会继续运行,程序将无法返回到命令提示符!

关于这一点,参见如下的一个原型测试代码:
#!/usr/bin/env python
# -*- encoding: utf-8 -*-

"""
Python treading daemonic:
If the second thread exits, the third thread will not exits,
no matter whether setDaemon(),
Only when the main thread(process) exists, the third one exists!
Author: Roc Zhou
Date: 2007-08-02
Email: chowroc.z@gmail.com
"""

import time
import threading

def run_thread():
while 1:
print "child thread is running ..."
time.sleep(1)

def run_schedule():
this = threading.Thread(target=run_thread, name="thread")
# this.setDaemon(1)
this.start()
print "schedule exiting ..."

that = threading.Thread(target=run_schedule, name="schedule")
# that.setDaemon(1)
that.start()
for i in range(10):
print "main thread is running ..."
time.sleep(1)
print "main thread exiting ..."
按几种不同的 setDaemon() 情况分别运行改程序:

星期二, 七月 31, 2007

python assemble methods at runtime?

在以前曾经讨论过一个例子:python unzip,当时在外部定义了一个函数,然后直接利用 setattr 将其加入到 ZipFile 类对象中,如下:
>>> class test:
... pass
...
>>> def func(self):
... print self.__dict__
...
>>> test.func = func
>>> t = test()
>>> t.func()
{}
>>>
但是,如果在类定义中直接使用,则不行:
>>> def func(self):
... print self.__dict__
...
>>> class test:
... def __init__(self):
... self.func = func
...
>>> t = test()
>>> t.func()
Traceback (most recent call last):
File "", line 1, in
TypeError: func() takes exactly 1 argument (0 given)
使用 setattr 也不行:
>>> def func(self):
... print self.__dict__
...
>>> class test:
... def __init__(self):
... setattr(self, 'func', func)
...
>>> t = test()
>>> t.func()
Traceback (most recent call last):
File "", line 1, in
TypeError: func() takes exactly 1 argument (0 given)
但是,是不是我放在外面定义就没有一点问题了呢?在 mirrord.py 中,我就希望在运行时再装配,因为直接在 __init__ 中定义不行,所以我这样做:
def _schedule_thread(self, Server, address):
that = threading.Thread(
target=self.__run_schedule, name='schedule', args=(Server, address))
that.setDaemon(1)
that.start()
that.join()

def _schedule_process(self, Server, address):
"""
There is a sharing problem has not been solved:
manager(WatchManager) should be shared among processes
"""
pid = os.fork()
if pid == 0: self.__run_schedule(Server, address)

class Mirrord:
...
def __run_schedule(self, Server, address):
...
Mirrord._schedule = _schedule_thread
结果在测试的时候报错:
Exception in thread mirrord:
Traceback (most recent call last):
File "/usr/lib/python2.5/threading.py", line 460, in __bootstrap
self.run()
File "/usr/lib/python2.5/threading.py", line 440, in run
self.__target(*self.__args, **self.__kwargs)
File "mirrord_ut.py", line 56, in __run_thread
self.mrd.run(('localhost', 2123))
File "/root/ulfs/cutils/trunk/lib/mirrord.py", line 497, in run
self._schedule(Server, address)
File "/root/ulfs/cutils/trunk/lib/mirrord.py", line 380, in _schedule_thread
target=self.__run_schedule, name='schedule', args=(Server, address))
AttributeError: Mirrord instance has no attribute '__run_schedule'
,而如果将那两个函数改为:
def _schedule_thread(self, Server, address):
that = threading.Thread(
target=self._Mirrord__run_schedule, name='schedule', args=(Server, address))
that.setDaemon(1)
that.start()
that.join()

def _schedule_process(self, Server, address):
"""
There is a sharing problem has not been solved:
manager(WatchManager) should be shared among processes
"""
pid = os.fork()
if pid == 0: self._Mirrord__run_schedule(Server, address)
则可以通过。但显然使用 _Mirrord__run_schedule 这种方式看上去不是太好!

python forking after threading?

现在需要对 mirrord 进行单元测试,但 mirrord 进行了 daemon switch,因此会 fork 子进程并退出父进程。而在单元测试中,我的测试程序只能通过创建子进程或子线程来运行工作代码(否则运行到 daemon 状态就阻塞进入无限循环,测试代码就没办法往下走了),为了得到工作代码的状态来进行检测,需要能够访问到工作代码的一些变量,所以简单的就是使用线程。

但问题是,如果在 threading 之后又进行了 forking,而 fork 后的父进程退出,那么线程是否退出呢?写一个原型代码看看:
#!/usr/bin/env python
# -*- encoding: utf-8 -*-

import os,sys
import time
import threading

def run_thread():
pid = os.fork()
if pid == 0:
print "child process ..."
while True:
time.sleep(5)
print "child process is running ..."
print "main process exiting ..."
sys.exit(0)

that = threading.Thread(target=run_thread, name="child")
that.setDaemon(1)
that.start()
# #1:
that.join()
print "main thread end"
运行结果如下:
sh$ python mirrord_5_ut.py
child process ...
main process exiting ...
main thread end
[Ctrl^C]
sh$ child process is running ...
child process is running ...
child process is running ...
[Ctrl^C]
sh$ child process is running ...
由此可见,fork 后的子进程还在运行,但是主进程退出了,也导致调度它的线程退出了!

但有一个问题,如果在 thread code 中 fork 的子进程也退出,那么在上面 #1 后面的代码会不会执行?因为进程是复制地址空间的,例如:
#!/usr/bin/env python
# -*- encoding: utf-8 -*-

"""
If the main process exits after forking,
while the forking is execute by a child thread,
will the main/child threads end? -- Yes
Author: Roc Zhou
Date: 2007-07-31
Email: chowroc.z@gmail.com
"""

import os,sys
import time
import threading

var = 0

def run_thread():
global var
var = 1
print "var is '%d' in thread" % var
pid = os.fork()
if pid == 0:
print "child process ..."
i = 0
while i < 10:
var = i * 2
time.sleep(1)
print "child process is running %d and var is '%d' ..." % (i, var)
i += 1
else:
print "main process ..."
j = 0
while j < 10:
var = j * 2 + 1
time.sleep(1)
print "main process is running %d and var is '%d' ..." % (j, var)
j += 1
# sys.exit(0)
print "var is '%d' at the end of thread code" % var

print "var is '%d' at the beginning" % var
# #a:
# run_thread()
# ------------
# #b:
that = threading.Thread(target=run_thread, name="child")
that.setDaemon(1)
that.start()
time.sleep(5)
# #1:
that.join()
# ------------
print "var is '%d' at the main thread end" % var
当按照 #a 来运行的时候,没有启动线程,那么输出结果是:
sh$ python mirrord_5_ut.py
var is '0' at the beginning
var is '1' in thread
child process ...
main process ...
main process is running 0 and var is '1' ...
child process is running 0 and var is '0' ...
main process is running 1 and var is '3' ...
child process is running 1 and var is '2' ...
main process is running 2 and var is '5' ...
child process is running 2 and var is '4' ...
main process is running 3 and var is '7' ...
child process is running 3 and var is '6' ...
main process is running 4 and var is '9' ...
child process is running 4 and var is '8' ...
main process is running 5 and var is '11' ...
child process is running 5 and var is '10' ...
main process is running 6 and var is '13' ...
child process is running 6 and var is '12' ...
main process is running 7 and var is '15' ...
child process is running 7 and var is '14' ...
main process is running 8 and var is '17' ...
child process is running 8 and var is '16' ...
main process is running 9 and var is '19' ...
var is '19' at the end of thread code
var is '19' at the main thread end
sh$ child process is running 9 and var is '18' ...
var is '18' at the end of thread code
var is '18' at the main thread end
可以看到,最后的代码运行了两次!但是在调用线程之后就不是,按 #b 运行,输出结果如下:
sh$ python mirrord_5_ut.py
var is '0' at the beginning
var is '1' in thread
child process ...
main process ...
child process is running 0 and var is '0' ...
main process is running 0 and var is '1' ...
child process is running 1 and var is '2' ...
main process is running 1 and var is '3' ...
child process is running 2 and var is '4' ...
main process is running 2 and var is '5' ...
child process is running 3 and var is '6' ...
main process is running 3 and var is '7' ...
child process is running 4 and var is '8' ...
main process is running 4 and var is '9' ...
child process is running 5 and var is '10' ...
main process is running 5 and var is '11' ...
child process is running 6 and var is '12' ...
main process is running 6 and var is '13' ...
child process is running 7 and var is '14' ...
main process is running 7 and var is '15' ...
child process is running 8 and var is '16' ...
main process is running 8 and var is '17' ...
child process is running 9 and var is '18' ...
var is '18' at the end of thread code
main process is running 9 and var is '19' ...
var is '19' at the end of thread code
var is '19' at the main thread end
这里最后的代码只运行了一次!

另外,是否使用 #1 处的 that.join() 也是有差别的!
sh$ python mirrord_5_ut.py
var is '0' at the beginning
var is '1' in thread
child process ...
main process ...
child process is running 0 and var is '0' ...
main process is running 0 and var is '1' ...
child process is running 1 and var is '2' ...
main process is running 1 and var is '3' ...
child process is running 2 and var is '4' ...
main process is running 2 and var is '5' ...
child process is running 3 and var is '6' ...
main process is running 3 and var is '7' ...
var is '9' at the main thread end
sh$ child process is running 4 and var is '8' ...
child process is running 5 and var is '10' ...
child process is running 6 and var is '12' ...
child process is running 7 and var is '14' ...
child process is running 8 and var is '16' ...
child process is running 9 and var is '18' ...
var is '18' at the end of thread code
所以,最终,将 mirrord.py 这个 module 中的 daemon 的代码移到 mirrord 这个 script 中!

星期一, 七月 30, 2007

python list pop operation 的一个典型

下面可能可以算得上是一个 List pop 操作的典型情况,是在一段单元测试代码中,我需要对某个目录下的文件进行查找,找到所有比指定时间要新的普通文件:
basedir = "/usr/local"
txlist = ["t:%s/etc" % basedir, "t:%s/share" % basedir, "x:%s/share/man"]

expect = []
for fname in self.__find_rfiles("%s/etc" % basedir): expect.append(fname)
for fname in self.__find_rfiles("%s/share" % basedir): expect.append(fname)
for fname in self.__find_rfiles("%s/share/man" % basedir): exclude.append(fname)
for fname in exclude: expect.remove(fname)
j = random.randint(0, len(expect))
ftemp = expect[j]
temp = os.lstat(ftemp)[-1]
ttemp = datetime.datetime(*time.localtime(temp)[:6])
rmidx = []
# #1:
for fname in expect:
temp = os.lstat(fname)[-1]
fctime = datetime.datetime(*time.localtime(temp)[:6])
if fctime > ttemp: rmidx.append(fname)
# #2:
for fname in rmidx:
expect.remove(fname)

fsbk = fs_backup.FsBackup(identity='test')
fsbk.append(txlist)
_files = fsbk.find(lasttime=ttemp)
_files.sort()
expect.sort()
self.failUnlessEqual(_files, expect)
self.__clean_last(fsbk)
这里,首先无法在位置 #1 处直接对 expect 进行 remove() 或 pop() 操作,因为这个操作会对当前列表的索引产生影响,导致当前位置指针指向了错误的项,所以会漏掉下一个节点,这在:python list pop in loop 中已经说明。

一种办法是使用在 python list pop in loop, 补充 中说明的方法,对指针进行调整。而这里采用另一种方法,建立一个不同的索引,每次都对重新寻找 expect 中指定节点的位置,这样指针位置就不会错了。

如果列表很大,那么每次搜寻索引可能会导致开销很大,那么可以考虑使用 dict(hash table) 来替换列表!

星期日, 七月 29, 2007

python list pop in loop, 补充

>>> L = ['La', 'Lb', 'c', 'Ld']
>>> x = 0
>>> for i in range(len(L)):
... print i
... if x: i -= 1
... if L[i][0] == 'L': L.pop(i); x = 1
... else: x = 0
...
0
'La'
1
'Lb'
2
'Ld'
3
Traceback (most recent call last):
File "", line 4, in ?
IndexError: list index out of range
这里如果要应用在多线程情况下,我考虑应该是将 x 扩展为一个队列,并应用生产者/消费者模式。

python variables sharing between threads

在 python list pop in loop 中说明了在循环中使用 pop 的问题,同时说明的多进程的情况。因为在 mirrod/fs_mirror 项目中必须使用共享 WatchManager,所以可以考虑先看看线程的情况。
#!/usr/bin/env python
# -*- encoding: utf-8 -*-

import sys
import threading
import time
import random

L = [ chr(i+97) for i in range(26) ]
print L

idx = 20

def run_thread():
# print "index: %d, %s" % (idx, L[idx])
for x in L:
i = L.index(x)
# print "thread: %d, %s" % (i, x)
time.sleep(1)
if i == idx:
for j in range(int(random.random() * 10000)): sys.stdout.write("*")
print
print "thread: %d, %s" % (i, x)
try:
i = L.index(x)
except ValueError:
print "index: %d, %s" % (i, L[i])
# Has been pop out
if i == idx + 1:
print "index + 1: %d, %s" % (i, L[i])
print "Child thread end"
# print "Child List:", L

srv = threading.Thread(target=run_thread, name='child')
srv.setDaemon(1)
srv.start()
# L.extend(['x', 'y', 'z'])
# time.sleep(3)
for x in L:
i = L.index(x)
# print "main: %d, %s" % (i, x)
time.sleep(1)
if i == idx:
for j in range(int(random.random() * 10000)): sys.stdout.write("-")
print
print "main: %d, %s" % (i, x)
print "pop %d, %s" % (i, L[i])
L.pop(i)
print "Main thread end"
# print "Main List:", L
srv.join()
父线程会从列表中取出 L[20],子线程在 L[20] 出打印结果,看输出结果:
sh$ python mirrord_4_sharing_thread.py
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
------...
main: 20, u
pop 20, u
******...
thread: 20, u
index: 20, v
index + 1: 21, w
Main thread end
Child thread end
可以看到,在 main thread 取出 20, 'u' 的同时,child thread 开始认为 20 是 'u',但马上由变成了 20, 'v',因为 20 在 for 中已经走过以此,因此在下一个循环中将直接进入 21,这是 'v' 就被 child thread 忽略了!

这里,必须考虑 Lock 的问题!

python list pop in loop

如果在一个列表中循环时做了 pop 操作,会有什么问题呢:
>>> L = ['a', 'b', 'c', 'd']
>>> for i in range(len(L)):
... x = L[i]
... if x == 'b': L.pop(i)
...
'b'
Traceback (most recent call last):
File "", line 2, in ?
IndexError: list index out of range
这里已经报错了。但如果换一种方法呢?
>>> L = ['a', 'b', 'c', 'd']
>>> for x in L:
... if x == 'b':
... i = L.index(x)
... L.pop(i)
...
'b'
>>> print L
['a', 'c', 'd']
这里看上去是对的,但会有潜在的问题?例如这样的情况:

>>> L = ['La', 'Lb', 'c', 'Ld']
>>> for x in L:
... if x[0] == 'L':
... i = L.index(x)
... L.pop(i)
...
'La'
'Ld'
>>> print L
['Lb', 'c']

>>> L = ['La', 'Lb', 'c', 'Ld']
>>> for x in L:
... if x[0] == 'L':
... L.remove(x)
...
>>> print L
['Lb', 'c']
那么如果是在多进程环境中呢?

看这个:
#!/usr/bin/env python
# -*- encoding: utf-8 -*-

import os,time

L = [ chr(i+97) for i in range(7) ]
print L
pid = os.fork()
if pid == 0:
for x in L:
print x
time.sleep(3)
print "Child process end"
print "Child List:", L
else:
L.extend(['x', 'y', 'z'])
j = 5
L.pop(j)
print "Main process end"
print "Main List:", L
其运行结果如下:
roc@crablfs:~/ulfs/cutils/trunk/prototypes$ python Ltest.py
['a', 'b', 'c', 'd', 'e', 'f', 'g']
a
Main process end
Main List: ['a', 'b', 'c', 'd', 'e', 'g', 'x', 'y', 'z']
roc@crablfs:~/ulfs/cutils/trunk/prototypes$ b
c
d
e
f
g
Child process end
Child List: ['a', 'b', 'c', 'd', 'e', 'f', 'g']
可以看到子进程没有受到影响,因为进程的地址空间是复制的,如果是线程 thread,就不存在这个问题。

于是,对于 mirrod/fs_mirrod 项目来说,因为 inotify 的 WatchManager 必须共享(父进程/线程写入,子进程/线程读取并根据这个变动的 manager 来建立 Processor),所以如果使用进程解决方案,就必须考虑共享问题;同时共享问题包括一个对资源的锁定问题,即父进程对共享内容的变更可能导致服务子进程/线程读取到的信息不准确!

星期五, 七月 27, 2007

python list comprehension and function

以下两种方式都是可以的!:
real_fnames.extend([os.path.normpath(f) for f in files])
real_fnames.extend(os.path.normpath(f) for f in files)