Python多线程threading的使用
一. threading的参数传递,参数之后的’,‘不能少,此处的’,‘是用来区分此参数作为元组(包含多个参数)来传递的,而不是单个参数传递
#coding:utf-
import threading
import time def b(arg):
time.sleep()
print("I'm B")
print("I'm", threading.current_thread().name)
print("I'm",arg)
time.sleep()
print("I'm sleep after") def a():
print("I'm A")
s = threading.Thread(target=b, name='LoopThread',args=("test",))
s.start()
print("I'm",threading.current_thread().name)
#time.sleep()
print("I'm C") print(a())
二. sleep的位置很重要,如上输出结果:
I'm A
I'm MainThread
I'm C
None
I'm B
I'm LoopThread
I'm test
I'm sleep after
第二种情况:
#coding:utf-
import threading
import time def b(arg):
#time.sleep(2)
print("I'm B")
print("I'm", threading.current_thread().name)
print("I'm",arg)
time.sleep()
print("I'm sleep after") def a():
print("I'm A")
s = threading.Thread(target=b, name='LoopThread',args=("test",))
s.start()
print("I'm",threading.current_thread().name)
#time.sleep()
print("I'm C") print(a())
输出结果:
I'm A
I'm BI'm
I'm LoopThread
I'm test
MainThread
I'm C
None
I'm sleep after
第三种情况:
#coding:utf-
import threading
import time def b(arg):
#time.sleep()
print("I'm B")
print("I'm", threading.current_thread().name)
print("I'm",arg)
time.sleep()
print("I'm sleep after") def a():
print("I'm A")
s = threading.Thread(target=b, name='LoopThread',args=("test",))
s.start()
print("I'm",threading.current_thread().name)
time.sleep()
print("I'm C") print(a())
输出结果:
I'm A
I'm BI'm
MainThread
I'm LoopThread
I'm test
I'm sleep after
I'm C
None
似乎其中一个线程稍微阻塞(或睡眠)就会立马开始执行另外一个,可以据此调控两个线程的交替?
关于线程的参数传递需要注意
Python多线程threading的使用的更多相关文章
- python多线程threading.Lock锁用法实例
本文实例讲述了python多线程threading.Lock锁的用法实例,分享给大家供大家参考.具体分析如下: python的锁可以独立提取出来 mutex = threading.Lock() #锁 ...
- [转]python 多线程threading简单分析
多线程和多进程是什么自行google补脑 对于python 多线程的理解,我花了很长时间,搜索的大部份文章都不够通俗易懂.所以,这里力图用简单的例子,让你对多线程有个初步的认识. 单线程 在好些年前的 ...
- Python多线程 - threading
目录 1. GIL 2. API 3. 创建子线程 4. 线程同步 4.1. 有了GIL,是否还需要同步? 4.1.1. 死锁 4.1.2. 竞争条件 4.1.3. GIL去哪儿了 4.2. Lock ...
- python多线程threading
本文通过 4个example 介绍python中多线程package —— threading的常用用法, 包括调用多线程, 同步队列类Queue, Ctrl+c结束多线程. example1. 调用 ...
- Python(多线程threading模块)
day27 参考:http://www.cnblogs.com/yuanchenqi/articles/5733873.html CPU像一本书,你不阅读的时候,你室友马上阅读,你准备阅读的时候,你室 ...
- [Python 多线程] threading.local类 (六)
在使用threading.local()之前,先了解一下局部变量和全局变量. 局部变量: import threading import time def worker(): x = 0 for i ...
- 再看python多线程------threading模块
现在把关于多线程的能想到的需要注意的点记录一下: 关于threading模块: 1.关于 传参问题 如果调用的子线程函数需要传参,要在参数后面加一个“,”否则会抛参数异常的错误. 如下: for i ...
- python多线程threading下载示例
#coding:utf-8 # windows中测试不通过,下载的图片不完整 # 通过多线程下载图片 import requests import threading class downloader ...
- python 多线程threading
上一篇说到thread模块,我们要自己解决线程锁.其实也没有什么啦.只是现在的人都比较懒,既然有高级封装的函数为什么要自己写. 所以就有了threading. 其实都一样啦. 来一个最简单的threa ...
- python 多线程threading的学习一
1. import threading #引入线程模块 2 申明实例 t = threading.Thread(target = fun, args = (,)) 说明:参数target 是要运行的 ...
随机推荐
- Android利用Mediapalyer播放本地资源文件声音
首先在res下创建raw,然后将mp3音乐拷贝到raw下 直接贴代码吧 //开始播放声音 public class PlayVoice { private static MediaPlayer med ...
- Object.assign方法的使用
https://www.cnblogs.com/chenyablog/p/6930596.html
- 自己手写一个queuelink
代码如下: <?php class QueueLink { private $first_key=0;//最前面的那个元素的键 private $first_value=0;//最前面的那个元素 ...
- 【中间件安全】WebSphere安全加固规范
1. 适用情况 适用于使用WebSphere进行部署的Web网站. 2. 技能要求 熟悉WebSphere安装部署,熟悉WebSphere常见漏洞利用方式,并能针对站点使用WebSphere进行安全加 ...
- java可供判断某字符串是什么编码的一行代码
System.out.println("中文"); System.out.println("中文".getBytes()); System.out.printl ...
- nginx配置-为没有后缀的文件(实际上是有html文件)以html形式打开
location ~ index.php@ { add_header content-type "text/html"; }
- Solve Error: "errcode": 85005, "errmsg": "appid not bind weapp hint"
在使用微信官方给的添加自定义菜单的示例代码: { "button":[ { "type":"click", "name" ...
- 【C++类与对象】实验四(二)
实现画图类 #ifndef GRAPH_H #define GRAPH_H // 类Graph的声明 class Graph { public: Graph(char ch, int n); // 带 ...
- MUI学习01-顶部导航栏
建议:先看一下MUI注意事项 连接:http://ask.dcloud.net.cn/article/122 固定栏靠前 所谓的固定栏,也就是带有.mui-bar属性的节点,都是基于fixed定位的元 ...
- 转 基于Excel参数化你的Selenium2测试
转载:https://blog.csdn.net/zhusongziye/article/details/80100375 前言 今天我们就如何使用xlrd模块来进行python selenium2 ...