博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python 进程与线程小随笔
阅读量:6441 次
发布时间:2019-06-23

本文共 3201 字,大约阅读时间需要 10 分钟。

Process

涉及模块:multiprocessing


  • Process

    • p = Process()
    • p.start()
    • p.join()
from multiprocessing import Processimport 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.')
  • Pool

    • p=Pool(5)
    • p.apply_async(func,**args)
    • p.close()
    • p.join()
from multiprocessing import Poolimport os, time, randomdef 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.')

Thread

涉及模块:threading

  • Thread
    • t = threading.Thread(target=xxx,name=xxx,args=(xxx,xxx))
    • t.start()
    • t.join()
    • threading.curent_thread().name
    • l = threading.Lock()
    • l.acquire()
    • l.release()
import time, threading# 新线程执行的代码:def loop(s):    print(s)    print('thread %s is running...' % threading.current_thread().name)    n = 0    while n < 5:        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', args=('a',))t.start()t.join()print('thread %s ended.' % threading.current_thread().name)
  • ThreadLocal
    • 线程中共享所有的全局变量,需注意是否会死锁
    • 局部只有当前线程可以进行操作,但调用会很麻烦
    • local = threading.local()
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()

第三方进程与线程模块

涉及模块:concurrent.futures

  • ThreadPoolExecutor、ProcessPoolExecutor
    • submit
    • map
    • concurrent.futures.as_completed
  • zip
    • 拼接
    • zip((1,2),(10,20)) ->((1,10),(2,20))
def sim_add(a):    return a[0]+a[1]    from concurrent.futures import ThreadPoolExecutorimport concurrent.futureswith ThreadPoolExecutor(max_workers=1) as t1:    future1 = t1.submit(sim_add,(1,2))    print(future1.result())print()var = [(1,2),(10,20),(100,200)]with ThreadPoolExecutor(max_workers=2) as t:    future = {t.submit(sim_add,tem): tem for tem in var}    for parameter,res in zip(var,concurrent.futures.as_completed(future)):        print("sim_add %s,return %s" % (parameter,res))    # 多次submit,不接收结果但是又想全部执行完    # concurrent.futures.wait()print()with ThreadPoolExecutor(max_workers=2) as t:    for f in t.map(sim_add,[(1,2),(2,3)]):        print(f)

转载于:https://www.cnblogs.com/simfg/p/7531881.html

你可能感兴趣的文章
IOS开发之表视图(UITableView)
查看>>
Notepad++去除代码行号的几种方法
查看>>
polay定理总结
查看>>
CodeForces 396C 树状数组 + DFS
查看>>
[sharepoint]rest api文档库文件上传,下载,拷贝,剪切,删除文件,创建文件夹,修改文件夹属性,删除文件夹,获取文档列表...
查看>>
远程桌面退出全屏/不能全屏/全屏切换的技巧
查看>>
【Java】Float计算不准确
查看>>
mybatis在xml文件中处理大于号小于号的方法
查看>>
Codeforces Codeforces Round #319 (Div. 2) A. Multiplication Table 水题
查看>>
各大浏览器CSS Hack收集
查看>>
再谈 $* 和 $@ 在 Bash 中的表现
查看>>
Apache Commons工具集简介
查看>>
【翻译】Nginx的反向代理
查看>>
htm、html、shtml网页区别
查看>>
SpringCloud学习笔记:服务注册与发现Eureka(2)
查看>>
学习新 api 的思考过程 4.18
查看>>
想要设计自己的微服务?看这篇文章就对了
查看>>
[译] 原生 JavaScript 值得学习吗?答案是肯定的
查看>>
29岁了还一事无成是人生的常态?
查看>>
gRPC-rs:从 C 到 Rust
查看>>