- 论坛徽章:
- 0
|
ftp_ins=ftplib.FTP()
ftp_ins.connect(host='127.0.0.1',port=21)
ftp_ins.login(user='ftp',passwd='ftp')
ftp_lock=threading.Lock()
def dir_walk(root_path):
dir_files = []
ftp_lock.acquire()
ftp_ins.cwd(root_path)
ftp_ins.dir(root_path,dir_files.append)
ftp_lock.release()
for item in dir_files[2:]:
file_type=item.split()[0][0]
file_name=item.split()[8]
if file_type=='d':
if root_path=='/':
dir_path=root_path+file_name
else:
dir_path=root_path+'/'+file_name
dir_deque.append(dir_path)
else:
pass
#设置ftp根目录
root_path='/'
#创建一个队列
dir_deque = deque()
dir_deque.append(root_path)
#通过循环队列的方式获取每次的root_path
def thread_process():
while True:
try:
root_path=dir_deque.popleft()
except:
break
else:
dir_walk(root_path)
print threading.current_thread()
time.sleep(0.5)
thread_num=2
threads=[]
num=0
for i in range(thread_num):
threads.append(threading.Thread(target=thread_process))
num+=1
print 'num:',num
for t in threads:
t.start()
for t in threads:
t.join()
运行结果
"D:\Program Files\Python2.7\python.exe" E:/WrokSpace/guang_dong_6_hundred_million/script/ftp2.py
num: 1
num: 2
<Thread(Thread-1, started 7984)>
<Thread(Thread-1, started 7984)>
<Thread(Thread-1, started 7984)>
<Thread(Thread-1, started 7984)>
<Thread(Thread-1, started 7984)>
<Thread(Thread-1, started 7984)>
<Thread(Thread-1, started 7984)>
|
|