1、threading

import threading
print threading.active_count()       #获取已激活的线程数
print threading.enumerate()          #查看所有线程信息
print threading.current_thread()         #查看现在正在运行的线程

多线程threading 随笔 第1张
#coding=utf-8
import threading
def thread_job():
    print('This is a thread of %s' % threading.current_thread())

def main():
    thread = threading.Thread(target=thread_job,)   # 定义线程 
    thread.start()  # 让线程开始工作
    
if __name__ == '__main__':
    main()
threading.Thread() 多线程threading 随笔 第3张
#coding=utf-8
import threading
def thread_job():
    print('This is a thread of %s' % threading.current_thread())
def main():
    thread = threading.Thread(target=thread_job,)   # 定义线程 
    thread.setDaemon(False)
    thread.start()  # 让线程开始工作
if __name__ == '__main__':
    print("1")
    main()
    print("2")
'''output:
1
2
This is a thread of <Thread(Thread-1, started 9424)>
'''

#coding=utf-8
import threading
def thread_job():
    print('This is a thread of %s' % threading.current_thread())
def main():
    thread = threading.Thread(target=thread_job,)   # 定义线程 
    thread.setDaemon(True)
    thread.start()  # 让线程开始工作
if __name__ == '__main__':
    print("1")
    main()
    print("2")
'''output:
1
2
'''
setDaemon 多线程threading 随笔 第5张
#coding=utf-8
import threading
def thread_job():
    print('This is a thread of %s' % threading.current_thread())

def main():
    thread = threading.Thread(target=thread_job,)   # 定义线程 
    thread.setDaemon(True)
    thread.start()  # 让线程开始工作
    thread.join()
if __name__ == '__main__':
    print("1")
    main()
    print("2")

'''output:
1
This is a thread of <Thread(Thread-1, started daemon 5292)>
2
'''

#coding=utf-8
import threading
def thread_job():
    print('This is a thread of %s' % threading.current_thread())

def main():
    thread = threading.Thread(target=thread_job,)   # 定义线程 
    #thread.setDaemon(True)
    thread.start()  # 让线程开始工作
    thread.join()
if __name__ == '__main__':
    print("1")
    main()
    print("2")
'''output:
1
This is a thread of <Thread(Thread-1, started 9220)>
2
'''

#coding=utf-8
import threading
def thread_job():
    print('This is a thread of %s' % threading.current_thread())

def main():
    thread = threading.Thread(target=thread_job,)   # 定义线程 
    #thread.setDaemon(True)
    thread.start()  # 让线程开始工作
    #thread.join()
if __name__ == '__main__':
    print("1")
    main()
    print("2")
'''output:
1
2
This is a thread of <Thread(Thread-1, started 10032)>
'''
join

1、setDaemon默认是False,先后关系不一定是按主程序至上向下执行---非阻塞式。

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
#coding=utf-8
import time
import threading
def thread_job():
    time.sleep(2)
    print('This is a thread of %s' % threading.current_thread())
def main():
    for i in range(4):
        thread = threading.Thread(target=thread_job,)   # 定义线程 
        #thread.setDaemon(False)
        thread.start()  # 让线程开始工作
if __name__ == '__main__':
    print("1")
    main()
    print("2")

'''output:
1
2
This is a thread of <Thread(Thread-2, started 7964)>This is a thread of <Thread(Thread-1, started 4108)>

This is a thread of <Thread(Thread-4, started 1540)>This is a thread of <Thread(Thread-3, started 8488)>
'''

2、join始终都是等子线程运行完才会退出程序(按主程序代码先后执行---阻塞式)

#coding=utf-8
import time
import threading
def thread_job():
    time.sleep(2)
    print('This is a thread of %s' % threading.current_thread())
def main():
    for i in range(4):
        thread = threading.Thread(target=thread_job,)   # 定义线程 
        #thread.setDaemon(True)
        thread.start()  # 让线程开始工作
        thread.join()
if __name__ == '__main__':
    print("1")
    main()
    print("2")

'''output:
1
This is a thread of <Thread(Thread-1, started 7904)>
This is a thread of <Thread(Thread-2, started 9136)>
This is a thread of <Thread(Thread-3, started 5480)>
This is a thread of <Thread(Thread-4, started 108)>
2
'''

2、Queue

在多线程函数中定义一个Queue,用来保存返回值,代替return,定义一个多线程列表,初始化一个多维数据列表。

多线程threading 随笔 第7张
import threading
import time

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()
Queue

3、线程锁

lock在不同线程使用同一共享内存时,能够确保线程之间互不影响,使用lock的方法是, 在每个线程执行运算修改共享内存之前,执行lock.acquire()将共享内存上锁, 确保当前线程执行时,内存不会被其他线程访问,执行运算完毕后,使用lock.release()将锁打开, 保证其他的线程可以使用该共享内存。

多线程threading 随笔 第9张
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()
    t1.join()
    t2.join()
threading.Lock()

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄