threading线程例子 (27-08)
利用阻塞的时间空闲去执行另一个子线程
import threading
from time import ctime, sleep def music(func):
for i in range(2):
print(func, ctime()) # 1 执行 # 4 执行
sleep(1) # 阻塞
print("end music", ctime()) # 3 执行 # 5 执行 def move(func):
for i in range(2):
print(func, ctime()) # 2 执行 # 7 执行
sleep(5) # 阻塞
print("end move", ctime()) # 6 执行 # 8 执行 threads=[]
t1 = threading.Thread(target=music,args=("小苹果",))
threads.append(t1)
t2 = threading.Thread(target=move,args=("华尔街之狼",))
threads.append(t2) if __name__ == "__main__":
for t in threads: # 遍历执行两个子线程
t.start()
# 整体执行时间为10秒
代码运行结果:
小苹果 Fri Sep 7 16:19:09 2018
华尔街之狼 Fri Sep 7 16:19:09 2018
end music Fri Sep 7 16:19:10 2018
小苹果 Fri Sep 7 16:19:10 2018
end music Fri Sep 7 16:19:11 2018
end move Fri Sep 7 16:19:14 2018
华尔街之狼 Fri Sep 7 16:19:14 2018
end move Fri Sep 7 16:19:19 2018
threading线程例子 (27-08)的更多相关文章
- python并发编程之threading线程(一)
进程是系统进行资源分配最小单元,线程是进程的一个实体,是CPU调度和分派的基本单位,它是比进程更小的能独立运行的基本单位.进程在执行过程中拥有独立的内存单元,而多个线程共享内存等资源. 系列文章 py ...
- Python学习笔记--threading线程
通过线程来实现多任务并发.提高性能.先看看例子. #!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2020-03-02 21:10:39 ...
- pythonl练习笔记——threading线程中的事件Event
1 事件Event 使用方法:e = threading.Event() Event对象主要用于线程间通信,确切地说是用于主线程控制其他线程的执行. Event事件提供了三个方法:wait等待.cle ...
- DELPHI线程例子-FC
{优秀的数据库应用应当充分考虑数据库访问的速度问题.通常可以通过优化数据库.优化 查询语句.分页查询等途径收到明显的效果.即使是这样,也不可避免地会在查询时闪现一个带有 SQL符号的沙漏,即鼠标变成了 ...
- 多进程 multiprocessing 多线程Threading 线程池和进程池concurrent.futures
multiprocessing.procsess 定义一个函数 def func():pass 在if __name__=="__main__":中实例化 p = process( ...
- Python Threading 线程/互斥锁/死锁/GIL锁
导入线程包 import threading 准备函数线程,传参数 t1 = threading.Thread(target=func,args=(args,)) 类继承线程,创建线程对象 class ...
- threading线程中的方法(27-11)
t1.start() # 执行线程 t1.join() # 阻塞 t1.setDaemon(True) #守护线程 threading.current_thread() # 查看执行的是哪一个线程 t ...
- import threading线程进程
cpu在执行一个子线程的时候遇到sleep就会利用这段停顿时间去执行另一个子线程.两个子线程谁先跳出sleep就执行谁. import threadingimport time start = tim ...
- java线程例子登山
Through its implementation, this project will familiarize you with the creation and execution of thr ...
随机推荐
- Java集合中的Map接口怎么使用?
Map(双列集合框架) 1.Map接口及实现类概述 Map 接口提供三种collection 视图,允许以键集.值集或键-值映射关系集的形式查看某个映射的内容.映射顺序 定义为迭代器在映射的 coll ...
- Batch - %~dp0 vs %cd%
总结 %~dp0 只表示将要“运行的”bat命令的folder,不包含bat自己的名称. %cd%表示,“运行处”的folder . 示例脚本内容 cd-dp0.bat存放在f盘 @echo off ...
- Go 动态类型声明
Go 动态类型声明 package main import "fmt" func main() { var x float64 = 20.0 y := 42 fmt.Println ...
- 压缩图片大小(Java源码)
/** * * 直接指定压缩后的宽高: * @param oldFile * 要进行压缩的文件 * @param width * 压缩后的宽度 * @param height * 压缩后的高度 * @ ...
- ETL工具-Kattle:查询 HTTP/WebService
发送HTTP POST请求,获取返回内容. 发送HTTP GET请求,获取返回内容,可以从前面获取URL.参数名.参数值 通过Restful获取数据 通过webService获取数据 HTTP ...
- Java-Class-C:org.springframework.http.HttpEntity
ylbtech-Java-Class-C:org.springframework.http.HttpEntity 1.返回顶部 1.1. import org.springframework.http ...
- Openstack-L 路由注入方式
目录 目录 前言 从 Commands 到 Action 操作函数 前言 Openstack 新旧版本提供了不同的路由注入方式,也就是 Route Module 的代码方式不同,就二次开发而言用那一种 ...
- 漏洞验证系列--MongoDB未授权访问
本系列文章旨在对于有一定网络安全基础的人员,在日常工作中扫描出来的各种漏洞,如何进行验证,以区分该漏洞是否存在或是扫描器误报.请勿应用非法途径. 本漏洞是由于MongoDB未设置访问权限,用户可以直接 ...
- jquery下拉框应用
<!DOCTYPE html> <html lang="en"> <head> <script src="http://code ...
- Codeforces Round #525 D - Ehab and another another xor problem /// 构造
题目大意: 本题有两个隐藏起来的a b(1<=a,b<=1e30) 每次可 printf("? %d %d\n",c,d); 表示询问 a^c 与 b^d 的相对大小 ...