python-day38(正式学习)
线程
线程开启的两种方式
1
from threading import Thread
import time
def test():
print('hello world')
t=Thread(target=test)
t.start()
print('hello')
time.sleep(1)
print('world')
2
from threading import Thread
import time
class mythread(Thread):
def run(self):
print('hello world')
time.sleep(1)
print('hhh')
m=mythread()
m.start()
print('ok')
子线程和子进程的创建速度
from threading import Thread
from multiprocessing import Process
import time
def test(name):
print('hello world',name)
time.sleep(1)
print('ok',name)
if __name__ == '__main__':
p=Process(target=test,args=('子进程',))
t=Thread(target=test,args=('子线程',))
print('zhu')
p.start()
t.start()
print('zhu1')
子线程共享资源
from threading import Thread
import os,time
x=100
def test():
global x
x=50
print('hello ',x)
print(os.getpid())
t=Thread(target=test)
t.start()
print('zhu ',x)
print(os.getpid())
线程的join方法
from multiprocessing import Process
from threading import Thread
import time
def task():
print('进程 开启')
time.sleep(10)
print('进程 结束')
def task2():
print('子线程 开启')
time.sleep(2)
print('子线程 结束')
if __name__ == '__main__':
p = Process(target=task)
t = Thread(target=task2)
t.start() # 开线程
p.start() # 开进程
print('子进程join开始')
p.join() # 主进程的主线程等待子进程运行结束
print('主')
守护线程
from threading import Thread
import time
def shouhu():
print('我是守护xian程')
time.sleep(10)
print('shouhu end')
t=Thread(target=shouhu)
t.daemon=True
t.start()
print('nice')
线程其他用法
from threading import Thread,currentThread,enumerate,activeCount
# import threading
import time
# threading.current_thread()
# threading.current_thread()
def task():
print('子线程 start')
time.sleep(2)
print('子线程 end')
print(enumerate())
# print(currentThread(),'子线程')
if __name__ == '__main__':
t1 = Thread(target=task)
t2 = Thread(target=task)
t1.start()
t2.start()
print(t1.is_alive()) # True
print(t1.getName()) # Thread-1
print(t2.getName()) # Thread-2
t1.setName('班长')
print(t1.getName())
print(currentThread().name)
print(enumerate()) # [<_MainThread(MainThread, started 1856)>, <Thread(Thread-1, started 6948)>, <Thread(Thread-2, started 3128)>]
print(activeCount()) # 3
print(len(enumerate())) # 3
python-day38(正式学习)的更多相关文章
- Python 装饰器学习
Python装饰器学习(九步入门) 这是在Python学习小组上介绍的内容,现学现卖.多练习是好的学习方式. 第一步:最简单的函数,准备附加额外功能 1 2 3 4 5 6 7 8 # -*- c ...
- Requests:Python HTTP Module学习笔记(一)(转)
Requests:Python HTTP Module学习笔记(一) 在学习用python写爬虫的时候用到了Requests这个Http网络库,这个库简单好用并且功能强大,完全可以代替python的标 ...
- 从Theano到Lasagne:基于Python的深度学习的框架和库
从Theano到Lasagne:基于Python的深度学习的框架和库 摘要:最近,深度神经网络以“Deep Dreams”形式在网站中如雨后春笋般出现,或是像谷歌研究原创论文中描述的那样:Incept ...
- Comprehensive learning path – Data Science in Python深入学习路径-使用python数据中学习
http://blog.csdn.net/pipisorry/article/details/44245575 关于怎么学习python,并将python用于数据科学.数据分析.机器学习中的一篇非常好 ...
- (转载)Python装饰器学习
转载出处:http://www.cnblogs.com/rhcad/archive/2011/12/21/2295507.html 这是在Python学习小组上介绍的内容,现学现卖.多练习是好的学习方 ...
- 正式学习React(五) react-redux源码分析
磨刀不误砍柴工,咱先把react-redux里的工具函数分析一下: 源码点这里 shallowEqual.js export default function shallowEqual(objA, ...
- 正式学习React(一) 开始学习之前必读
为什么要加这个必读!因为webpack本身是基于node环境的, 里面会涉及很多路径问题,我们可能对paths怎么写!webpack又是怎么找到这些paths的很迷惑. 本文是我已经写完正式学习Rea ...
- python网络爬虫学习笔记
python网络爬虫学习笔记 By 钟桓 9月 4 2014 更新日期:9月 4 2014 文章文件夹 1. 介绍: 2. 从简单语句中開始: 3. 传送数据给server 4. HTTP头-描写叙述 ...
- Python装饰器学习
Python装饰器学习(九步入门) 这是在Python学习小组上介绍的内容,现学现卖.多练习是好的学习方式. 第一步:最简单的函数,准备附加额外功能 ? 1 2 3 4 5 6 7 8 # -*- ...
- Python的基础学习(第二周)
模块初始 sys模块 import sys sys.path #打印环境变量 sys.argv#打印该文件路径 #注意:该文件名字不能跟导入模块名字相同 os模块 import os cmd_res ...
随机推荐
- Codeforces 785 E. Anton and Permutation(分块,树状数组)
Codeforces 785 E. Anton and Permutation 题目大意:给出n,q.n代表有一个元素从1到n的数组(对应索引1~n),q表示有q个查询.每次查询给出两个数l,r,要求 ...
- liunx 一些文件下载上传的命令
xshell 上传 rz sftp 语法: put path/filename.txt Put -r path/文件夹 或者 put “path/filename.txt” Put -r “path/ ...
- $\LaTeX$数学公式大全8
$8\ Miscellaneous\ symbols$ $\infty$ \infty $\nabla$ \nabla $\partial$ \partial $\eth$ \eth $\clubsu ...
- UDP和TCP浅析
UDP协议全称是用户数据报协议,在网络中它与TCP协议一样用于处理数据包,是一种无连接的协议. 在选择使用协议的时候,选择UDP必须要谨慎.在网络质量令人十分不满意的环境下,UDP协议数据包丢失会比较 ...
- MyBatis 整合 Druid
pom.xml 依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=" ...
- koa 基础(二十六)数据库 与 art-template 模板 联动 --- 编辑数据、删除数据
1.通过 ObjectID 获取 _id 根目录/module/db.js /** * DB库 */ var MongoDB = require('mongodb'); var MongoClient ...
- java实现一个简单的验证码生成器
最近看了网上很多大佬们写的验证码生成,寻思着自己也写一个,话不多说,代码如下: import java.awt.BasicStroke; import java.awt.Color; import j ...
- Carousel 走马灯
在有限空间内,循环播放同一类型的图片.文字等内容 基础用法 适用广泛的基础用法 结合使用el-carousel和el-carousel-item标签就得到了一个走马灯.幻灯片的内容是任意的,需要放在e ...
- [SQL]学习中遇到的错误
1.中英文模式切换 查找语句中不能出现中文字符!!!
- windows 重启java进程脚本
这个脚本用于启动和重启javaWeb程序 @echo off rem 配置端口号 set port= rem 第一层循环检查端口占用的pid for /f "tokens=5" % ...