Python Multiprocessing问题

为什么我创建多进程后,子进程运行完成后不自动退出来啊,而是变成死进程,为 Sl+状态,只有父进程完成后,子进程才退出?如何让子进程完成后自动退出来啊。

给个示例:


# -*- coding:utf-8 -*-
import thread,threading
import time
 
def FuncTest(tdata):
    print tdata
     
class mythread(threading.Thread):
    def __init__(self,threadname):
        threading.Thread.__init__(self)
 
    def run(self):
        lock.acquire()
        FuncTest(ft)
        lock.release()
         
def MutiThread(num):
    threads=[]
    i=0
    global ft
    for x in xrange(num):
        threads.append(mythread(num))
    for t in threads:
        time.sleep(0.5)
        lock.acquire()
        ft=GetThreadParam(datafile,num,i)
        #print '[%s]Thread:%s,Testdata:%s'%(time.ctime(),t,ft)
        i=i+1
        t.start() 
        lock.release()
    for t in threads:
        t.join()
 
def GetThreadParam(datafile, num, curthread):
    #线程数需要小于文件行数
    f=open(datafile,'r')
    lines=f.readlines()
    divres=divmod(len(lines),num)
    if curthread<(num-1):
        res=lines[curthread*divres[0]:(curthread+1)*divres[0]]
    elif curthread==(num-1):
        res=lines[curthread*divres[0]:((curthread+1)*divres[0]+divres[1])]
    return res
    f.close()
     
if __name__ == '__main__':
     
    global num,lock
    datafile='a.txt'
     
    num=3  #num å¹¶å‘æ•°
     
    lock=threading.Lock()
    MutiThread(num)

a.txt文件内容如下

1

2

3

4

5

6

7

8

9

10


3个线程并发时,运行结果:

>>> 

['1\n', '2\n', '3\n']

['4\n', '5\n', '6\n']

['7\n', '8\n', '9\n', '10']

温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-08-15
把错误截出来!!!