最近在用python的多线程处理一些数据。
import threading
import time
def thread_job(): #该任务比较消耗时间
print("T1 start\n")
for i in range(10):
time.sleep(0.1)
print("T1 finish\n")
def T2_job():
print("T2 start\n")
print("T2 finish\n")
def main():
added_thread=threading.Thread(target=thread_job,name="T1")
thread2=threading.Thread(target=T2_job,name="T2")
added_thread.start()
thread2.start()
thread2.join() #join的作用是不让主线程比某个thread先结束
print("all done\n")
if __name__ == '__main__':
main()
import threading
import time
li = [0 for i in range(12)]
class MyThread(threading.Thread):
def __init__(self, threadId, name, counter):
threading.Thread.__init__(self)
self.threadId = threadId;
self.name = name
self.counter = counter
def run(self):
print("开启线程" + self.name)
#threadLock.acquire()
print_time(self.name,self.counter,len(li))
#threadLock.release()
def __del__(self):
print(self.name+"线程结束!")
def print_time(threadName, delay, counter):
while counter:
time.sleep(delay)
li[counter - 1] += 1
print("[%s] %s修改%d个值,修改后为:%d" % (time.ctime(time.time()), threadName, counter, li[counter - 1]))
counter -= 1
threadLock = threading.Lock()
threads = []
thread1 = MyThread(1, "Thread-1", 1)
thread2 = MyThread(2, "Thread-2", 2)
thread1.start()
thread2.start()
threads.append(thread1)
threads.append(thread2)
for t in threads:
t.join()
print("主进程结束")
import threading
from queue import Queue
def job(li,q):
for i in range(len(li)):
li[i]=li[i]**2
q.put(li)
def multithreading():
q=Queue()
threads=[]
data=[[1,2,3],[3,4,5],[5,6,7],[7,8,9]]
for i in range(4):
t=threading.Thread(target=job,args=(data[i],q))
t.start()
threads.append(t)
for thread in threads:
thread.join()
results=[]
for _ in range(4):
results.append(q.get())
print(results)
if __name__ == '__main__':
multithreading()
import threading
from queue import Queue
import copy
import time
def job(li,q):
res=sum(li)
q.put(res)
def multithreading(l):
q=Queue()
threads=[]
for i in range(4):
t=threading.Thread(target=job,args=(copy.copy(l),q))
t.start()
threads.append(t)
[t.join() for t in threads]
total=0
for _ in range(4):
total+=q.get()
print(total)
def normal(l):
total=sum(l)
print(total)
if __name__=="__main__":
l=list(range(1000000))
s_time=time.time()
normal(l*4)
print("normal "+str(time.time()-s_time))
s_time=time.time()
multithreading(l)
print("multithreading: "+str(time.time()-s_time))
输出如下:多线程的速度确实比普通的快一些,我在IDE里运行的差距不明显
import threading
def job1():
global A,lock
lock.acquire()
for i in range(10):
A+=1
print("job1",A)
lock.release()
def job2():
global A,lock
lock.acquire()
for i in range(10):
A+=10
print("job2",A)
lock.release()
if __name__=='__main__':
lock=threading.Lock()
A=0
t1=threading.Thread(target=job1)
t2=threading.Thread(target=job2)
t1.start()
t2.start()
import threading
def job1():
global A
for i in range(10):
A+=1
print("job1",A)
def job2():
global A
for i in range(10):
A+=10
print("job2",A)
if __name__=='__main__':
A=0
t1=threading.Thread(target=job1)
t2=threading.Thread(target=job2)
t1.start()
t2.start()
t2.join()
t3=threading.Thread(target=job1)
t3.start()