多线程 threading
1、观察线程

import threading
def thread_job():
    print(threading.current_thread())
def main():
    print(threading.active_count())#打印threading的数目
    print(threading.enumerate())#当前线程名字
    print(threading.current_thread())#当前线程名字
    add_threading=threading.Thread(target=thread_job)
    add_threading.start()#开始
if __name__ == "__main__":
    main()

2、jion线程同步

import threading
import time
def thread_job():
    print("T1 START\n")
    time.sleep(0.2)
    print(threading.current_thread())
def main():
    add_threading=threading.Thread(target=thread_job,name="T1")
    add_threading.start()
    add_threading.join()
    print("all done")
if __name__ == "__main__":
    main()

3、queue线程队列

import threading
import time
from queue import Queue
def job(l,q):
    print(threading.enumerate())#当前线程名字
    time.sleep(1)
    for i in range(len(l)):
        l[i]=l[i]**2
    q.put(l)
def multithreading():
    q=Queue()
    threads=[]
    data=[[1,2,34,5,6,7,8,9,0,11,13],[2,4],[4,5,6],[5,6,7,8,9,0]]
    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()

4、线程锁lock

import threading
import time
def job1():
    global a,lock
    lock.acquire()
    for i in range(10):
        a=a+10
        print("job1",a)
    lock.release()

def job2():
    global a,lock
    lock.acquire()
    for i in range(10):
        a=a+50
        print("job2",a)
    lock.release()

if __name__ == "__main__":
    lock=threading.Lock()
    a=0
    t1=threading.Thread(target=job1,name="job1")
    t2=threading.Thread(target=job2,name="job2")
    t1.start()
    t2.start()
    t1.join()
    t2.join()
    print("result")