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 是要运行的 ...
随机推荐
- 【thinkphp5.1】 endroid/qrcode 二维码生成
composer 链接: https://packagist.org/packages/endroid/qrcode 注意:PHP版本 要求 7.1+ 1. 使用 composer 安装 endroi ...
- C# 网络爬虫利器之Html Agility Pack如何快速实现解析Html
简介 现在越来越多的场景需要我们使用网络爬虫,抓取相关数据便于我们使用,今天我们要讲的主角Html Agility Pack是在爬取的过程当中,能够高效的解析我们抓取到的html数据. 优势 在.NE ...
- mysql之表格的关联关系
1.’基本模式有多对一,多对多,一对一.关联的两个基本组建为外键列和参照列 典型的多对一模式,很普遍,如部门表和员工表,即一个部门可以有多个员工. 对于多对多的模式,就需要建立中间表,将其转换为多对一 ...
- B - Battle City bfs+优先队列
来源poj2312 Many of us had played the game "Battle city" in our childhood, and some people ( ...
- 阿里天池的新任务(简单)(KMP统计子串出现的次数)
阿里“天池”竞赛平台近日推出了一个新的挑战任务:对于给定的一串 DNA 碱基序列 tt,判断它在另一个根据规则生成的 DNA 碱基序列 ss 中出现了多少次. 输出格式 输出一个整数,为 tt 在 s ...
- 将 Entity Framework、LINQ 和 Model-First 用于 Oracle 数据库
目的 本教程介绍如何使用 Entity Framework.语言集成查询 (LINQ),以及如何对 Oracle 数据库使用 Model-First 以生成数据定义语言 (DDL) 脚本. 所需时间 ...
- Jquery 获取 radio选中值,select选中值
随着Jquery的作用越来越大,使用的朋友也越来越多.在Web中,由于CheckBox.Radiobutton .DropDownList等控件使用的频率比较高,就关系到这些控件在Jquery中的操作 ...
- hive reduce 阶段GC Exception
某个reduce中的value堆积的对象过多,导致jvm频繁GC. 解决办法: 1. 增加reduce个数,set mapred.reduce.tasks=300,. 2. 在hive-site.xm ...
- ruby离线安装整理
参考官方文档: https://rvm.io/rvm/offline 参考博客:https://blog.csdn.net/topswim/article/details/79260369 一.前提 ...
- Android开发网【申明:来源于网络】
Android开发网[申明:来源于网络] 地址:http://www.jizhuomi.com/android/video/