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 是要运行的 ...
随机推荐
- 深度学习中交叉熵和KL散度和最大似然估计之间的关系
机器学习的面试题中经常会被问到交叉熵(cross entropy)和最大似然估计(MLE)或者KL散度有什么关系,查了一些资料发现优化这3个东西其实是等价的. 熵和交叉熵 提到交叉熵就需要了解下信息论 ...
- [原]win10下编译lua5.3.4
1.下载lua源码http://www.lua.org/ftp/ 2.打开vs2012工具命令提示 3.cd 到lua源码的src目录 4.依次执行以下代码 cl /MD /O2 /c /DLUA_B ...
- LVS & NGINX
LVS NGINX
- puppet(2)-资源介绍
puppet- 资源介绍: 类型.属性与状态同实现方式分离.仅指定目标状态 type {'title': attribute => value, ... } 查看支持的资源类型: puppet ...
- 关于Kafka producer管理TCP连接的讨论
在Kafka中,TCP连接的管理交由底层的Selector类(org.apache.kafka.common.network)来维护.Selector类定义了很多数据结构,其中最核心的当属java.n ...
- [git/GitHub] git push 时报错:fatal: remote error: You can't push to git://github.com/user/xxx.git(已解决)
当使用 git push 时,提示以下错误: fatal: remote error: You can't push to git://github.com/user/xxx.git Use ht ...
- 19款Windows实用软件推荐,满满的干货,总有一款是你必备的
https://post.smzdm.com/p/745799/ 追加修改(2018-08-20 12:28:23):一些追加内容: 很多人都在吐槽为什么推荐Clover,这里我说明一下,就我了解到的 ...
- Spark Distributed matrix 分布式矩阵
RowMatrix行矩阵 import org.apache.spark.rdd.RDD import org.apache.spark.mllib.linalg.Vectors import org ...
- K - Popular Cows
来源poj2186 Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N ...
- 树pao(雾)
今天难得睡醒了,大概睡了13个小时,打算今晚把树剖学了. 嘿嘿嘿雀魂真好玩,这篇文章咕了 有学上了,找到了水平远高于我的队友,好开心的说,,, 卡空间感觉治不了了... 板子题是洛谷P3384 实在不 ...