1. 程式人生 > >Python 3 之多執行緒研究

Python 3 之多執行緒研究

今天想寫一個工具,通過多執行緒去一個佇列中讀取資料,要求如下:

1.多個執行緒同時讀取佇列,所以佇列要做到執行緒安全:queue.Queue,這個本身就是執行緒安生的,所以沒有問題

2.主執行緒要等到所有新開的子執行緒結束後才能結束,這個用到了Threading中的isAlive()方法,來判斷執行緒是否還存活

3.不能出現開了多個子執行緒,卻被一個搶著CPU執行時間,這個學過作業系統的都知道time.sleep(0.01),執行緒停止一下,讓出CPU下面是程式碼了

'''
Created on 2011-11-10

@author: PaulWang

Description:

FileName:myThread.py
'''
import threading
import time

class MyThread(threading.Thread):
    def __init__(self, threadname,records):
        threading.Thread.__init__(self, name=threadname)
        self.records = records
        self.name = threadname

    def run(self):
#        time.sleep(0.01)
        print("downloadFromQueue start %d" % self.records.qsize())
        while(self.records.qsize()):
            print( "id:%s get item is %s" % (self.name, self.records.get()) )
            time.sleep(0.001)

'''
Created on 2011-11-9

@author: PaulWang

Description:

FileName:test.py
'''

from queue import Queue
import myMySQL
import threading
import time
from myThread import MyThread

try:

    def download(records,threadNum):
        tasks = []

        for i in range(0,threadNum):
            Thread = MyThread(i,records)
            Thread.setDaemon( False )#主執行緒結束後子執行緒結束整個程序才結束
            Thread.start()
            tasks.append(Thread)

        #如果有執行緒沒有結束,那就不退出,確保主執行緒不退出
        for task in tasks:
            if task.isAlive():
                tasks.append(task)
                continue

    db = myMySQL.myMySQL();
    db.connect( "localhost","root","","cdcol",True )
    db.selectDB("drupal")
    rows = db.getrows("select * from test")
    print(rows)

    myQueue = Queue()
    for row in rows:
        myQueue.put(row)

    download(myQueue,4)   

    if db.isConnected():
        db.close()

    print('is over...')

except:
    if db.isConnected():
        db.close()
        print("error ")