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之多线程的更多相关文章

  1. day33 python学习 多线程

    线程的概念 进程只是用来把资源集中到一起(进程只是一个资源单位,或者说资源集合),而线程才是cpu上的执行单位. 三 线程与进程的区别 1 1.线程的创建开销小(无需申请内存空间或者资源),创建线程的 ...

  2. Python的多线程(threading)与多进程(multiprocessing )

    进程:程序的一次执行(程序载入内存,系统分配资源运行).每个进程有自己的内存空间,数据栈等,进程之间可以进行通讯,但是不能共享信息. 线程:所有的线程运行在同一个进程中,共享相同的运行环境.每个独立的 ...

  3. Python GIL 多线程机制 (C source code)

    最近阅读<Python源码剖析>对进程线程的封装解释: GIL,Global Interpreter Lock,对于python的多线程机制非常重要,其如何实现的?代码中实现如下: 指向一 ...

  4. Python实现多线程HTTP下载器

    本文将介绍使用Python编写多线程HTTP下载器,并生成.exe可执行文件. 环境:windows/Linux + Python2.7.x 单线程 在介绍多线程之前首先介绍单线程.编写单线程的思路为 ...

  5. Python实现多线程调用GDAL执行正射校正

    python实现多线程参考http://www.runoob.com/python/python-multithreading.html #!/usr/bin/env python # coding: ...

  6. Python之多线程和多进程

    一.多线程 1.顺序执行单个线程,注意要顺序执行的话,需要用join. #coding=utf-8 from threading import Thread import time def my_co ...

  7. python的多线程到底有没有用?

    在群里经常听到这样的争执,有人是虚心请教问题,有人就大放厥词因为这个说python辣鸡.而争论的核心无非就是,python的多线程在同一时刻只会有一条线程跑在CPU里面,其他线程都在睡觉.这是真的吗? ...

  8. 通过编写聊天程序来熟悉python中多线程及socket的用法

    1.引言 Python中提供了丰富的开源库,方便开发者快速就搭建好自己所需要的应用程序.本文通过编写基于tcp/ip协议的通信程序来熟悉python中socket以及多线程的使用. 2.python中 ...

  9. Python的多线程和多进程

    (1)多线程的产生并不是因为发明了多核CPU甚至现在有多个CPU+多核的硬件,也不是因为多线程CPU运行效率比单线程高.单从CPU的运行效率上考虑,单任务进程及单线程效率是最高的,因为CPU没有任何进 ...

  10. Python【多线程与多进程】

    import time,threading print("=======串行方式.并行两种方式调用run()函数=======")def run(): print('哈哈哈') # ...

随机推荐

  1. 检测是否安装或者开启flash

    function flashChecker() { var hasFlash = 0; //是否安装了flash var flashVersion = 0; //flash版本 if(document ...

  2. Bi-Directional ConvLSTM U-Net with Densley Connected Convolutions

    Bi-Directional ConvLSTM U-Net with Densley Connected Convolutions  ICCV workshop 2019  2019-09-15 11 ...

  3. ASP.NET中httpmodules与httphandlers全解析

    https://www.cnblogs.com/zpc870921/archive/2012/03/12/2391424.html https://www.cnblogs.com/PiaoMiaoGo ...

  4. 浏览器低延时播放监控摄像头RTSP海康大华硬盘录像机NVR视频(EasyNVR播放FLV视频流)

    背景描述 EasyNVR的使用者应该都是清楚的知道,EasyNVR一个强大的功能就是可以进行全平台的无插件直播.主要原因在于rtsp协议的视频流(默认是需要插件才可以播放的)经由EasyNVR处理后可 ...

  5. [LeetCode] 568. Maximum Vacation Days 最大化休假日

    LeetCode wants to give one of its best employees the option to travel among N cities to collect algo ...

  6. python:单元测试框架pytest的一个简单例子

    之前一般做自动化测试用的是unitest框架,发现pytest同样不错,写一个例子感受一下 test_sample.py import cx_Oracle import config from sen ...

  7. Metricbeat 轻量型指标采集器

    一.介绍 用于从系统和服务收集指标.从 CPU 到内存,从 Redis 到 Nginx,Metricbeat 能够以一种轻量型的方式,输送各种系统和服务统计数据. 1.系统级监控,更简洁(轻量型指标采 ...

  8. SecureCRT 使用密钥登录 Ubuntu

    记录 SecureCRT 通过 SSH 使用密钥登录 Ubuntu. 具体步骤如下: 1. 使用 SecureCRT 生成密钥对: 工具 -> 创建公钥 -> 密钥类型 RSA -> ...

  9. PHP设计模式 - 模板方法模式

    模板模式准备一个抽象类,将部分逻辑以具体方法以及具体构造形式实现,然后声明一些抽象方法来迫使子类实现剩余的逻辑.不同的子类可以以不同的方式实现这些抽象方法,从而对剩余的逻辑有不同的实现.先制定一个顶级 ...

  10. 【剑指offer】面试题 24. 反转链表

    面试题 24. 反转链表