day33-python之多线程
1.多线程实例
# import threading
# import time
#
#
import threading
import time
class MyThread(threading.Thread):
def __init__(self,num):
threading.Thread.__init__(self)
self.num = num def run(self):
print("running on number:%s"%self.num)
time.sleep(3) if __name__ == '__main__':
t1 = MyThread(1)
t2 = MyThread(2) t1.start()
t2.start()
print("ending")
# class MyThread(threading.Thread):
# def __init__(self, num):
# threading.Thread.__init__(self)
# self.num = num
#
# def run(self): # 定义每个线程要运行的函数
#
# print("running on number:%s" % self.num)
#
# time.sleep(3)
#
#
# if __name__ == '__main__':
# t1 = MyThread(1)
# t2 = MyThread(2)
# t1.start()
# t2.start()
#
# print("ending......")
2.join
# import threading
# import time
#
#
# def music():
# print("begin to listen %s"%time.ctime())
# time.sleep(3)
# print("stop to listen %s" % time.ctime())
#
#
# def game():
# time.sleep(4)
# t3=threading.Thread(target=music)
# t3.start()
#
# print("begin to play game %s"%time.ctime())
# time.sleep(5)
# print("stop to play game %s" % time.ctime())
# #
# if __name__ == '__main__':
#
# t1= threading.Thread(target=music)
#
# t2 = threading.Thread(target=game)
#
# t1.start()
# t2.start()
#
# t1.join()
# t2.join()
#
# print("ending")
# # import threading
# from time import ctime,sleep
# import time
#
# def ListenMusic(name):
#
# print ("Begin listening to %s. %s" %(name,ctime()))
# sleep(3)
# print("end listening %s"%ctime())
# # def RecordBlog(title):
#
# print ("Begin recording the %s! %s" %(title,ctime()))
# sleep(5)
# print('end recording %s'%ctime())
#
# threads = []
#
# t1 = threading.Thread(target=ListenMusic,args=('水手',))
# t2 = threading.Thread(target=RecordBlog,args=('python线程',))
#
# threads.append(t1)
# threads.append(t2) # if __name__ == '__main__':
# #t1.setDaemon(True)
# t2.setDaemon(True)
#
# for t in threads:
# #t.setDaemon(True) #注意:一定在start之前设置
# t.start()
# print(t.getName())
# print("count:",threading.active_count())
# #t.join()#串行
# #t.join()
#
# #t1.join()
# #t1.setDaemon(True)
#
# #t2.join()########考虑这三种join位置下的结果?
#
# while threading.active_count()==1:
#
# print ("all over %s" %ctime()) # 调用方式2:####################################### # import threading
# import time
# import threading
import time
class MyThread(threading.Thread):
def __init__(self,num):
threading.Thread.__init__(self)
self.num = num def run(self):
print("running on number:%s"%self.num)
time.sleep(3) if __name__ == '__main__':
t1 = MyThread(1)
t2 = MyThread(2)
t1.start()
t2.start()
print("ending")
# class MyThread(threading.Thread):
#
# def __init__(self, num):
# threading.Thread.__init__(self)
# self.num = num
#
# def run(self): # 定义每个线程要运行的函数
#
# print("running on number:%s" % self.num)
#
# time.sleep(3)
#
# if __name__ == '__main__':
#
# t1 = MyThread(1)
# t2 = MyThread(2)
# t1.start()
# t2.start()
# print("ending......")
day33-python之多线程的更多相关文章
- day33 python学习 多线程
线程的概念 进程只是用来把资源集中到一起(进程只是一个资源单位,或者说资源集合),而线程才是cpu上的执行单位. 三 线程与进程的区别 1 1.线程的创建开销小(无需申请内存空间或者资源),创建线程的 ...
- Python的多线程(threading)与多进程(multiprocessing )
进程:程序的一次执行(程序载入内存,系统分配资源运行).每个进程有自己的内存空间,数据栈等,进程之间可以进行通讯,但是不能共享信息. 线程:所有的线程运行在同一个进程中,共享相同的运行环境.每个独立的 ...
- Python GIL 多线程机制 (C source code)
最近阅读<Python源码剖析>对进程线程的封装解释: GIL,Global Interpreter Lock,对于python的多线程机制非常重要,其如何实现的?代码中实现如下: 指向一 ...
- Python实现多线程HTTP下载器
本文将介绍使用Python编写多线程HTTP下载器,并生成.exe可执行文件. 环境:windows/Linux + Python2.7.x 单线程 在介绍多线程之前首先介绍单线程.编写单线程的思路为 ...
- Python实现多线程调用GDAL执行正射校正
python实现多线程参考http://www.runoob.com/python/python-multithreading.html #!/usr/bin/env python # coding: ...
- Python之多线程和多进程
一.多线程 1.顺序执行单个线程,注意要顺序执行的话,需要用join. #coding=utf-8 from threading import Thread import time def my_co ...
- python的多线程到底有没有用?
在群里经常听到这样的争执,有人是虚心请教问题,有人就大放厥词因为这个说python辣鸡.而争论的核心无非就是,python的多线程在同一时刻只会有一条线程跑在CPU里面,其他线程都在睡觉.这是真的吗? ...
- 通过编写聊天程序来熟悉python中多线程及socket的用法
1.引言 Python中提供了丰富的开源库,方便开发者快速就搭建好自己所需要的应用程序.本文通过编写基于tcp/ip协议的通信程序来熟悉python中socket以及多线程的使用. 2.python中 ...
- Python的多线程和多进程
(1)多线程的产生并不是因为发明了多核CPU甚至现在有多个CPU+多核的硬件,也不是因为多线程CPU运行效率比单线程高.单从CPU的运行效率上考虑,单任务进程及单线程效率是最高的,因为CPU没有任何进 ...
- Python【多线程与多进程】
import time,threading print("=======串行方式.并行两种方式调用run()函数=======")def run(): print('哈哈哈') # ...
随机推荐
- 视频色彩空间RGB、YUV、YCbCr
RGB.YUV和YCbCr都是人为规定的彩色模型或颜色空间(有时也叫彩色系统或彩色空间).它的用途是在某些标准下用通常可接受的方式对彩色加以描述.本质上,彩色模型是坐标系统和子空间的阐述. RGB R ...
- Python推荐一整套开发工具
原文:https://sourcery.ai/blog/python-best-practices/ 在开始一个新的Python项目时,很容易不做规划直接进入编码环节.花费少量时间,用最好的工具设置项 ...
- spring cloud Eureka server 问题 Spring Cloud java.lang.TypeNotPresentException
版本: spring-cloud.version : Greenwich.SR2 pom配置: <project xmlns="http://maven.apache.org/POM/ ...
- AES的256位密钥加解密报 java.security.InvalidKeyException: Illegal key size or default parameters 异常的处理及处理工具
一.出现的现象为了数据代码在传输过程中的安全,很多时候我们都会将要传输的数据进行加密,然后等对方拿到后再解密使用.我们在使用AES加解密的时候,在遇到128位密钥加解密的时候,没有进行什么特殊处理:然 ...
- 一起学习log4cxx
目前成熟的日志系统有很多,比如log4cxx,log4cpp等,今天一起来学习log4cxx吧,之所以学习这个,首先,这个日志库比较成熟,一直由apach基金在维护,而log4cpp缺乏维护.再者,这 ...
- [LeetCode] 245. Shortest Word Distance III 最短单词距离 III
This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as ...
- JIT(just in time)即时编译器
JIT(just in time) 前端vs后端 在编译原理中,通常将编译分为前端和后端.其中前端会对程序进行词法分析.语法分析.语义分析,然后生成一个中间表达形式(称为IR:Intermediate ...
- ou can mix require and export. You can't mix import and module.exports.
ou can mix require and export. You can't mix import and module.exports.
- centos 如何修改docker镜像和容器的默认存放路径
原因:通过df -h查看磁盘利用的时候,目前挂载的太小了,所以尝试挂载到其他地方 1 先看看默认存放的路径在哪儿 方法1:docker info 方法2:sudo docker info | grep ...
- Java程序运行机制
Java程序运行机制 编译型(compile) 它有一个负责翻译的程序(编译器),将我们写的 Java 源代码转为计算机可执行的代码 举个例子:把一本中文书翻译成英文书 应用:操作系统.C.C++ 解 ...