线程

线程开启的两种方式

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(正式学习)的更多相关文章

  1. Python 装饰器学习

    Python装饰器学习(九步入门)   这是在Python学习小组上介绍的内容,现学现卖.多练习是好的学习方式. 第一步:最简单的函数,准备附加额外功能 1 2 3 4 5 6 7 8 # -*- c ...

  2. Requests:Python HTTP Module学习笔记(一)(转)

    Requests:Python HTTP Module学习笔记(一) 在学习用python写爬虫的时候用到了Requests这个Http网络库,这个库简单好用并且功能强大,完全可以代替python的标 ...

  3. 从Theano到Lasagne:基于Python的深度学习的框架和库

    从Theano到Lasagne:基于Python的深度学习的框架和库 摘要:最近,深度神经网络以“Deep Dreams”形式在网站中如雨后春笋般出现,或是像谷歌研究原创论文中描述的那样:Incept ...

  4. Comprehensive learning path – Data Science in Python深入学习路径-使用python数据中学习

    http://blog.csdn.net/pipisorry/article/details/44245575 关于怎么学习python,并将python用于数据科学.数据分析.机器学习中的一篇非常好 ...

  5. (转载)Python装饰器学习

    转载出处:http://www.cnblogs.com/rhcad/archive/2011/12/21/2295507.html 这是在Python学习小组上介绍的内容,现学现卖.多练习是好的学习方 ...

  6. 正式学习React(五) react-redux源码分析

    磨刀不误砍柴工,咱先把react-redux里的工具函数分析一下: 源码点这里  shallowEqual.js export default function shallowEqual(objA, ...

  7. 正式学习React(一) 开始学习之前必读

    为什么要加这个必读!因为webpack本身是基于node环境的, 里面会涉及很多路径问题,我们可能对paths怎么写!webpack又是怎么找到这些paths的很迷惑. 本文是我已经写完正式学习Rea ...

  8. python网络爬虫学习笔记

    python网络爬虫学习笔记 By 钟桓 9月 4 2014 更新日期:9月 4 2014 文章文件夹 1. 介绍: 2. 从简单语句中開始: 3. 传送数据给server 4. HTTP头-描写叙述 ...

  9. Python装饰器学习

    Python装饰器学习(九步入门)   这是在Python学习小组上介绍的内容,现学现卖.多练习是好的学习方式. 第一步:最简单的函数,准备附加额外功能 ? 1 2 3 4 5 6 7 8 # -*- ...

  10. Python的基础学习(第二周)

    模块初始 sys模块 import sys sys.path #打印环境变量 sys.argv#打印该文件路径 #注意:该文件名字不能跟导入模块名字相同 os模块 import os cmd_res ...

随机推荐

  1. 2.nohup和&后台运行,进程查看及终止

    1.nohup和& 语法:nohup Command [ Arg … ] [& ] nohup:不挂断地运行命令 &:在后台运行 示例:nohup java -jar app2 ...

  2. Android_(控件)使用自定义控件在屏幕中绘制一条虚线

    在Android屏幕中绘制虚线,最通用的是自定义控件DashedLine,再将自定义控件放入xml布局中 运行截图: 程序结构 package com.example.asus.gary_042; i ...

  3. PHP-配置MySQL

    安装mysql 修改PHP配置文件 修改php安装路径下 php.ini extension=php_mysqli.dll 在代码路径下添加php文件,在里面编辑 <?php phpinfo() ...

  4. pyton3的数字操作你都会用吗?

    '''数字数据类型用于存储数值.数据类型是不允许改变的,这就意味着如果改变数字数据类型的值,将重新分配空间. 1.del(用于删除一些数字对象的引用) 2.整形(int)通常被称为是整形或者整数,是正 ...

  5. Leetcode题目31.下一个排列(中等)

    题目描述: 实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列. 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列). 必须原地修改,只允许使用额外 ...

  6. websocket聊天体验(二)

    上一篇说到后续可以支持:最近历史.表情+图片,顺便还实现了简易的音频和视频.暂时没有实现实时语音对讲,有待后续再研究.点开在线聊天页面,即可看到最近历史记录(18条). 聊天的底层数据都是基于txt文 ...

  7. IP输出 之 ip_output、ip_finish_output、ip_finish_output2

    概述 ip_output-设置输出设备和协议,然后经过POST_ROUTING钩子点,最后调用ip_finish_output: ip_finish_output-对skb进行分片判断,需要分片,则分 ...

  8. 连续子数组的最大和 java实现

    package findMax; /** * 连续子数组的最大和 * @author root * */ public class FindMax { static int[] data = {1,- ...

  9. LC 959. Regions Cut By Slashes

    In a N x N grid composed of 1 x 1 squares, each 1 x 1 square consists of a /, \, or blank space.  Th ...

  10. 自定义View绘制简单的圆环的实现

    package com.loaderman.mywave; import android.content.Context; import android.graphics.Canvas; import ...