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

Posted mohu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python核心编程笔记4--多线程相关的知识,希望对你有一定的参考价值。

单线程程序:

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--多线程的主要内容,如果未能解决你的问题,请参考以下文章

Python 3多线程编程学习笔记-基础篇

python核心编程笔记----threading

Python笔记:多线程

Python核心编程总结(五多任务编程之进程与线程)

Python核心编程总结(五多任务编程之进程与线程)

经典干货《Java 多线程编程核心技术》学习笔记及总结(中)