码迷,mamicode.com
首页 > 编程语言 > 详细

python核心编程笔记4--多线程

时间:2018-04-20 13:30:57      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:import   div   img   http   end   pre   class   图片   []   

单线程程序:

import time
def Sorry():
    print "Im sorry!"    time.sleep(1)
if __name__==__main__:
    for i in range(5):
        Sorry()

添加线程


import threading

def thread_job():
print ‘this is an added Thread,number is %s‘ % threading.current_thread()

if __name__ == ‘__main__‘:
added_thread = threading.Thread(target=thread_job)
added_thread.start()

join 等待线程结束之后再往下执行

import time,threading
def thread_job():
    print T1 start\n
    for i in range(10):
        time.sleep(0.1)
    print T1 finish\n

def mian():
    added_thread = threading.Thread(target=thread_job,name=T1)
    added_thread.start()
    print all done\n
if __name__ == __main__:
    mian()
import time,threading
def thread_job():
    print T1 start\n
    for i in range(10):
        time.sleep(0.1)
    print T1 finish\n

def mian():
    added_thread = threading.Thread(target=thread_job,name=T1)
    added_thread.start()
    added_thread.join()
    print all done\n
if __name__ == __main__:
    mian()

Queue功能,因为线程中不能使用return

import time,threading
from queue import Queue
def job(l,q):
    for i in range(len(l)):
        l[i] = l[i]**2
    q.put(l)
def multithreading():
    q = Queue()
    threads = []
    data = [[1,2,3],[3,4,5],[4,4,4],[5,5,5]]
    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()

lock锁

import threading
def job1():
    global A
    for i in range(10):
        A += 1
        print job1 %d % A
def job2():
    global A
    for i in range(10):
        A += 10
        print job2 %d % A

if __name__ == __main__:
    A = 0
    t1 = threading.Thread(target=job1)
    t2 = threading.Thread(target=job2)
    t1.start()
    t2.start()
    t1.join()
    t2.join()
import threading
def job1():
    global A,lock
    lock.acquire()
    for i in range(10):
        A += 1
        print job1 %d % A
    lock.release()
def job2():
    global A,lock
    lock.acquire()
    for i in range(10):
        A += 10
        print job2 %d % 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()
    t1.join()
    t2.join()

ps:多线程不一定有效率,有GIL锁的存在,python只能同一时间让一个线程运算同一个运算

import threading
from queue import Queue
import copy
import time

def job(l, q):
    res = sum(l)
    q.put(res)

def multithreading(l):
    q = Queue()
    threads = []
    for i in range(4):
        t = threading.Thread(target=job, args=(copy.copy(l), q), name=T%i % i)
        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_t = time.time()
    normal(l*4)
    print(normal: ,time.time()-s_t)
    s_t = time.time()
    multithreading(l)
    print(multithreading: , time.time()-s_t)

技术分享图片

 

python核心编程笔记4--多线程

标签:import   div   img   http   end   pre   class   图片   []   

原文地址:https://www.cnblogs.com/mohu/p/8889665.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!