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. java excel给单元格增加批注(包含SXSSF)

    package javatest; import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi ...

  2. Python模块安装方法

    安装Python模块 电子邮件 distutils-sig @ python .组织 作为一个受欢迎的开源开发项目,Python具有活跃的贡献者和用户支持社区,并且根据开放源代码许可条款,其软件可供其 ...

  3. 二进制和ASCII文件的区别

    二进制和ASCII文件的区别 觉得有用的话,欢迎一起讨论相互学习~Follow Me 版权声明:本文为CSDN博主「迂者-贺利坚」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出 ...

  4. [LeetCode] 217. Contains Duplicate 包含重复元素

    Given an array of integers, find if the array contains any duplicates. Your function should return t ...

  5. [LeetCode] 499. The Maze III 迷宫 III

    There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...

  6. [LeetCode] 805. Split Array With Same Average 用相同均值拆分数组

    In a given integer array A, we must move every element of A to either list B or list C. (B and C ini ...

  7. js时间的一些处理

    1.判断是否为同一天 function isSameFay(t) { return new Date(t).toDateString() === new Date().toDateString(); ...

  8. Centos7修改profile错误的解救方案

    在改profile的时候,改出问题了,除了cd以外的命令基本都不能用了.使用root用户执行: # export PATH=/usr/bin:/usr/sbin:/bin:/sbin:/usr/X11 ...

  9. 客户端请求服务器端通信, Web 编程发展基础|乐字节

    乐字节的小伙伴们,好久不见,甚是想念啊! 前面我发布的文章算是把Java初级基础阶段讲完了,接下来小乐将会给大家接着讲Java中级阶段——Javaweb. 首先,我们要看看Javaweb阶段主要重点掌 ...

  10. 【转帖】微软全新Windows 10X细节一览

    微软全新Windows 10X细节一览 https://www.cnbeta.com/articles/tech/906241.htm windows NT之后 又一大改进 今年的Surface发布会 ...