【廖雪峰老师python教程】——进程与线程
2021-05-16 06:28
标签:sse time 创建 死锁 现在 关闭 子进程 释放 running 多线程编程,模型复杂,容易发生冲突,必须用锁加以隔离,同时,又要小心死锁的发生。 Python解释器由于设计时有GIL全局锁,导致了多线程无法利用多核。多线程的并发在Python中就是一个美丽的梦 在多线程环境下,每个线程都有自己的数据。一个线程使用自己的局部变量比使用全局变量好,因为局部变量只有线程自己能看见,不会影响其他线程,而全局变量的修改必须加锁。 但是局部变量也有问题,就是在函数调用的时候,传递起来很麻烦: 一个 【廖雪峰老师python教程】——进程与线程 标签:sse time 创建 死锁 现在 关闭 子进程 释放 running 原文地址:https://www.cnblogs.com/ChaoyuanJam/p/9749718.html多进程
multiprocessing
模块就是跨平台版本的多进程模块
from multiprocessing import Process
import os
# 子进程要执行的代码
def run_proc(name):
print(‘Run child process %s (%s)...‘ % (name, os.getpid()))
if __name__==‘__main__‘:
print(‘Parent process %s.‘ % os.getpid())
p = Process(target=run_proc, args=(‘test‘,))
print(‘Child process will start.‘)
p.start()
p.join()#等待子进程结束,才继续往下进行
print(‘Child process end.‘)
Parent process 928.
Process will start.
Run child process test (929)...
Process end.
from multiprocessing import Pool
import os, time, random
def long_time_task(name):
print(‘Run task %s (%s)...‘ % (name, os.getpid()))
start = time.time()
time.sleep(random.random() * 3)
end = time.time()
print(‘Task %s runs %0.2f seconds.‘ % (name, (end - start)))
if __name__==‘__main__‘:
print(‘Parent process %s.‘ % os.getpid())
p = Pool(4)
for i in range(5):
p.apply_async(long_time_task, args=(i,))
print(‘Waiting for all subprocesses done...‘)
p.close()# 必须先关闭,才能停止添加进程,才能运行
p.join()
print(‘All subprocesses done.‘)
Parent process 669.
Waiting for all subprocesses done...
Run task 0 (671)...
Run task 1 (672)...
Run task 2 (673)...
Run task 3 (674)...
Task 2 runs 0.14 seconds.
Run task 4 (673)...
Task 1 runs 0.27 seconds.
Task 3 runs 0.86 seconds.
Task 0 runs 1.41 seconds.
Task 4 runs 1.91 seconds.
All subprocesses done.
多线程
import time, threading
# 新线程执行的代码:
def loop():
print(‘thread %s is running...‘ % threading.current_thread().name)
n = 0
while n :
n = n + 1
print(‘thread %s >>> %s‘ % (threading.current_thread().name, n))
time.sleep(1)
print(‘thread %s ended.‘ % threading.current_thread().name)
print(‘thread %s is running...‘ % threading.current_thread().name)
t = threading.Thread(target=loop, name=‘LoopThread‘)
t.start()
t.join()
print(‘thread %s ended.‘ % threading.current_thread().name)
balance = 0
lock = threading.Lock()
def run_thread(n):
for i in range(100000):
# 先要获取锁:
lock.acquire()
try:
# 放心地改吧:
change_it(n)
finally:
# 改完了一定要释放锁:
lock.release()
ThreadLocal
ThreadLocal
变量虽然是全局变量,但每个线程都只能读写自己线程的独立副本,互不干扰。ThreadLocal
解决了参数在一个线程中各个函数之间互相传递的问题。import threading
# 创建全局ThreadLocal对象:
local_school = threading.local()
def process_student():
# 获取当前线程关联的student:
std = local_school.student
print(‘Hello, %s (in %s)‘ % (std, threading.current_thread().name))
def process_thread(name):
# 绑定ThreadLocal的student:
local_school.student = name
process_student()
t1 = threading.Thread(target= process_thread, args=(‘Alice‘,), name=‘Thread-A‘)
t2 = threading.Thread(target= process_thread, args=(‘Bob‘,), name=‘Thread-B‘)
t1.start()
t2.start()
t1.join()
t2.join()
Hello, Alice (in Thread-A)
Hello, Bob (in Thread-B)
分布式进程
文章标题:【廖雪峰老师python教程】——进程与线程
文章链接:http://soscw.com/index.php/essay/86131.html