并发

基本概念

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

并发和并行的区别

 Python进阶13---并发和线程 随笔 第1张

Python进阶13---并发和线程 随笔 第2张

Python进阶13---并发和线程 随笔 第3张

Python进阶13---并发和线程 随笔 第4张

并发的解决

Python进阶13---并发和线程 随笔 第5张

Python进阶13---并发和线程 随笔 第6张

Python进阶13---并发和线程 随笔 第7张

Python进阶13---并发和线程 随笔 第8张

Python进阶13---并发和线程 随笔 第9张

Python进阶13---并发和线程 随笔 第10张

进程和线程

Python进阶13---并发和线程 随笔 第11张

Python进阶13---并发和线程 随笔 第12张

线程的状态

Python进阶13---并发和线程 随笔 第13张

Python进阶13---并发和线程 随笔 第14张

Python进阶13---并发和线程 随笔 第15张

Python中的进程和线程

Python进阶13---并发和线程 随笔 第16张

Python的线程开发

Python进阶13---并发和线程 随笔 第17张

Thread类

Python进阶13---并发和线程 随笔 第18张

 Python进阶13---并发和线程 随笔 第19张

线程启动

import threading
import time
#最简单的线程程序
def worker():
    for _ in range(5):
        time.sleep(0.5)
        print('welcome ')
        print('Thread over')

t = threading.Thread(target=worker)#线程对象
t.start()#启动

Python进阶13---并发和线程 随笔 第20张

import threading
import time
#最简单的线程程序
def worker():
    while True:
        time.sleep(0.5)
        print("I'm working")
    print('Thread over')

t = threading.Thread(target=worker)#线程对象
t.start()#启动

线程退出

Python进阶13---并发和线程 随笔 第21张

import threading
import time

def worker():
    count = 0
    while True:
        if (count>5):
            raise RuntimeError(count)
        time.sleep(1)
        print("I'm working")
        count += 1

t = threading.Thread(target=worker)#线程对象
t.start()#启动
print('===End===')

Python进阶13---并发和线程 随笔 第22张

线程的传参

import threading
import time

def add(x,y):
    print(threading.current_thread())
    print('{}+{}={} {}'.format(x,y,x+y,threading.current_thread().ident))

thread1 = threading.Thread(target=add,name='add1',args=(4,5))#线程对象
thread1.start()
time.sleep(2)

thread2 = threading.Thread(target=add,name='add2',args=(5,),kwargs={'y':4})#线程对象
thread2.start()
time.sleep(2)

thread3 = threading.Thread(target=add,name='add3',kwargs={'x':4,'y':5})
thread3.start()

Python进阶13---并发和线程 随笔 第23张

threading的属性和方法

Python进阶13---并发和线程 随笔 第24张

#示例1
import threading
import time

def worker(n=5):
    print("in",threading.current_thread())
    print(threading.main_thread())
    print(threading.active_count())
    print(threading.enumerate())
    for _ in range(n):
        time.sleep(0.5)
        print('welcome')
    print('Thread over')

print("out",threading.current_thread())

t = threading.Thread(target=worker,name='w1')
t.start()

# t1 = threading.Thread(target=worker,name='w2')
# t1.start()

print(threading.enumerate())
#[<_MainThread(MainThread, started 18880)>, <Thread(w1, started 7404)>]或者
# [<_MainThread(MainThread, stopped 18720)>, <Thread(w1, started 22448)>]
#示例2
import threading
import time

def showthreadinfo(n=5):
    print("currentthread ={}".format(threading.current_thread()))
    print("main thread = {}".format(threading.main_thread()))
    print("active count ={}".format(threading.active_count()))
    print(threading.enumerate())

def worker():
    count = 0
    showthreadinfo()
    while True:
        if count>5:
            break
        time.sleep(0.5)
        count += 1
        print("I'm working")


t = threading.Thread(target=worker,name='worker')#线程对象
showthreadinfo()
t.start()
#此时输出如下:
currentthread =<_MainThread(MainThread, started 18900)>
main thread = <_MainThread(MainThread, started 18900)>
active count =1
[<_MainThread(MainThread, started 18900)>]
currentthread =<Thread(worker, started 19512)>
main thread = <_MainThread(MainThread, stopped 18900)>
active count =2
[<_MainThread(MainThread, stopped 18900)>, <Thread(worker, started 19512)>]
...

Thread实例的属性和方法

Python进阶13---并发和线程 随笔 第25张

Python进阶13---并发和线程 随笔 第26张

 

import threading
import time

def worker():
    count = 0
    while True:
        if count>5:
            break
        time.sleep(0.5)
        count += 1
        print(threading.current_thread().name)

t = threading.Thread(target=worker,name='worker')#线程对象
print(11,t.ident)#输出None,因为还未启动该线程
t.start()

while True:
    time.sleep(1)
    if t.is_alive():
        print('{} {} alive'.format(t.name,t.ident))
    else:
        print('{} {} dead'.format(t.name,t.ident))
        t.start()#可以否?#不可以,RuntimeError: threads can only be started once

Python进阶13---并发和线程 随笔 第27张

Python进阶13---并发和线程 随笔 第28张

start方法

import threading
import time

class MyThread(threading.Thread):
    def run(self):
        print('run')
        super().run()

    def start(self):
        print('start')
        return super().start()

def worker():
    count = 0
    while True:
        if count>2:
            break
        time.sleep(0.5)
        count += 1
        print('working')
    print('Thread over')

t = MyThread(target=worker,name='w1')
t.start()
#输出如下:
start
run
working
working
working
Thread over

run方法

import threading
import time

class MyThread(threading.Thread):
    def run(self):
        print('run')
        super().run()

    def start(self):
        print('start')
        return super().start()

def worker():
    count = 0
    while True:
        if count>2:
            break
        time.sleep(0.5)
        count += 1
        print('working')
    print('Thread over')

t = MyThread(target=worker,name='w1')
t.run()
#输出如下:
run
working
working
working
Thread over

Python进阶13---并发和线程 随笔 第29张

start和run的区别

Python进阶13---并发和线程 随笔 第30张

import threading
import time

class MyThread(threading.Thread):
    def run(self):
        print('run')
        super().run()

    def start(self):
        print('start')
        return super().start()

def worker():
    count = 0
    while True:
        if count>2:
            break
        time.sleep(0.5)
        count += 1
        print('working')
    print('Thread over')
    print(threading.current_thread().name)

t = MyThread(target=worker,name='w1')
# t.run()#输出MainThread
t.start()#输出w1

Python进阶13---并发和线程 随笔 第31张

多线程

Python进阶13---并发和线程 随笔 第32张

import threading
import time

def worker():
    count = 0
    while True:
        if count>5:
            break
        time.sleep(0.5)
        count += 1
        print('working')
    print('Thread over')
    print(threading.current_thread().name,threading.current_thread().ident)

class MyThread(threading.Thread):
    def run(self):
        print('run')
        super().run()

    def start(self):
        print('start')
        return super().start()


t1 = MyThread(target=worker,name='worker1')
t2 = MyThread(target=worker,name='worker2')
t1.start()
t2.start()
# t1.run()
# t2.run()

Python进阶13---并发和线程 随笔 第33张

Python进阶13---并发和线程 随笔 第34张

待续。

 

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