Python的threading

基础用法, 通过 threading.Thread() 创建线程, 然后 start() 和 join()

import time
import threading def do_something(seconds):
print('Sleeping...')
time.sleep(seconds)
print('Done') start = time.perf_counter()
threads = [] for _ in range(10):
t = threading.Thread(target = do_something, args=[1])
t.start()
threads.append(t) for t in threads:
t.join() finish = time.perf_counter()
print('Total: {}'.format(round(finish - start, 2)))

使用线程池. 使用as_completed, 可以阻塞并按完成顺序输出结果, 而直接用executor.map()会将结果收集完成后一起返回.

import time
import threading
from concurrent import futures def do_something(seconds):
print('Sleeping...')
time.sleep(seconds)
return 'Done ' + str(seconds) start = time.perf_counter()
with futures.ThreadPoolExecutor(max_workers=3) as executor:
secs = [3, 2.5, 2, 2.2, 0.5]
results = [executor.submit(do_something, sec) for sec in secs]
for f in futures.as_completed(results):
print(f.result()) # 注意区别
with futures.ThreadPoolExecutor() as executor:
secs = [3, 2.5, 2, 2.2, 0.5]
# 下面这行实际是阻塞的
results = executor.map(do_something, secs)
for result in results:
print(result) finish = time.perf_counter()
print('Total: {}'.format(round(finish - start, 2)))

.

Python的multiprocessing

.在使用multiprocessing时, 子进程里的print()是会滞后打印的.

import time
import multiprocessing
import logging def do_something(seconds):
print('Sleeping...', seconds)
time.sleep(seconds)
return 'Done ' + str(seconds) if __name__ == '__main__':
multiprocessing.log_to_stderr()
logger = multiprocessing.get_logger()
logger.setLevel(logging.INFO)
start = time.perf_counter()
secs = [3.1, 3.5, 3.1, 3.2, 3.5, 3.3]
processes = []
for sec in secs:
p = multiprocessing.Process(target=do_something, args=(sec,))
p.start()
processes.append(p) for p in processes:
p.join() finish = time.perf_counter()
print('Total: {}'.format(round(finish - start, 2)))
print() pool = multiprocessing.Pool(processes=3)
print(pool.map(do_something, secs))
finish = time.perf_counter()
print('Total: {}'.format(round(finish - start, 2)))

.

Python的threading和multiprocessing的更多相关文章

  1. Python 线程(threading) 进程(multiprocessing)

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

  2. python的threading和multiprocessing模块初探

    转载于:http://blog.csdn.net/zhaozhi406/article/details/8137670

  3. python 标准库 —— 线程与同步(threading、multiprocessing)

    1. 创建线程 使用 os 下的 fork() 函数调用(仅限 Unix 系统) import os print('current process (%s) starts ...' % (os.get ...

  4. python并发编程之multiprocessing进程(二)

    python的multiprocessing模块是用来创建多进程的,下面对multiprocessing总结一下使用记录. 系列文章 python并发编程之threading线程(一) python并 ...

  5. Python 线程(threading)

    Python 的thread模块是比较底层的模块,Python的threading模块是对thread做了一些包装,可以更加方便的 被使用; 1. 使用threading 模块 # 示例一: 单线程执 ...

  6. Python之threading多线程,多进程

    1.threading模块是Python里面常用的线程模块,多线程处理任务对于提升效率非常重要,先说一下线程和进程的各种区别,如图 概括起来就是 IO密集型(不用CPU) 多线程计算密集型(用CPU) ...

  7. Python多线程 - threading

    目录 1. GIL 2. API 3. 创建子线程 4. 线程同步 4.1. 有了GIL,是否还需要同步? 4.1.1. 死锁 4.1.2. 竞争条件 4.1.3. GIL去哪儿了 4.2. Lock ...

  8. python中threading的用法

    摘自:http://blog.chinaunix.net/uid-27571599-id-3484048.html 以及:http://blog.chinaunix.net/uid-11131943- ...

  9. python中threading模块详解(一)

    python中threading模块详解(一) 来源 http://blog.chinaunix.net/uid-27571599-id-3484048.html threading提供了一个比thr ...

随机推荐

  1. GitHub Vue项目推荐|Vue+Element实现的电商后台管理系统功能丰富

    GitHub Vue项目推荐|mall-admin-web是一个电商后台管理系统的前端项目基于Vue+Element实现 主要包括商品管理.订单管理.会员管理.促销管理.运营管理.内容管理.统计报表. ...

  2. Jquery。

    Jquery: 1.概念:JavaScript的框架.本质上就是一些JS文件,封装了JS的原生代码而已. 2.快速入门:下载Jquery -导入文件-使用. 3.JQ对象和JS对象的区别. * :Jq ...

  3. git commit 之后,撤销commit操作

    撤销.修改commit 写代码过程中,如果已经git add [files] git -m commit [files],没有push代码到远程仓库,想撤销commit,可以根据实际情况,使用以下参数 ...

  4. 马哥docker听课记录

    容器技术:chroot.namespaces.cgroups docker平时用户空间只运行一个进程,只运行在一个namespaces中 镜像:分层构建.联合挂载 容器编排工具:kubernetes ...

  5. 安装VMware Tools的步骤

    点击[虚拟机]选项中的[安装VMware Tools],此时在Ubuntu的桌面上就会出现一个光盘图标. 如果之前已经安装过了,[虚拟机]选项中应为[重新安装VMware Tools]. 如果[重新安 ...

  6. 离线环境下自动化部署python环境(含openssl)

    遇到有项目要在内网环境下安装python项目,所以空余时写了自动化部署python环境和python项目的脚本,由于项目涉密,这里仅提供自动化部署python环境的shell脚本,包括openssl的 ...

  7. 手抄吧1:windows web server

    字母写的他妈的 太恶心了  以后努力改  天天敲代码  好恶心的字体

  8. canvas小案列-绚丽多彩的倒计时

    本次随笔中,我将实现一个绚丽的倒计时效果,这个效果主要是结合canvas和js实现的,具体代码如下 index.html文件 <!DOCTYPE html> <html> &l ...

  9. 软件测试之Monkey 初步了解(入门级II)

    1. 先熟悉monkey基本命令: cls   清除 首先测试设备是否连接成功,在命令行中输入:adb devices 查看adb版本: adb version 查看虚拟机版本:nox_adb ver ...

  10. Mysql复制一条或多条记录并插入表|mysql从某表复制一条记录到另一张表

    Mysql复制一条或多条记录并插入表|mysql从某表复制一条记录到另一张表 一.复制表里面的一条记录并插入表里面    ① insert into article(title,keywords,de ...