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. vue+elementui搭建后台管理界面(6登录和菜单权限控制)

    不同的权限对应不同的路由(菜单),同时侧边栏也根据权限异步生成,实现登录和鉴权思路如下: 登录:点击登录,服务器验证通过后返回一个 token ,然后存到 cookie,再根据 token 拉取用户权 ...

  2. 如何开发一个npm包并发布到npm中央仓库

    转自: https://liaolongdong.com/2019/01/24/publish-public-npm.html 如何开发一个npm包并发布到npm中央仓库需求背景:平时在项目工作中可能 ...

  3. Python装饰器之functools.wraps的作用

    # -*- coding: utf-8 -*- # author:baoshan def wrapper(func): def inner_function(): pass return inner_ ...

  4. httpcomponent框架MultipartEntityBuilder addTextBody中文乱码

    版本4.5.6 String url = "https://172.16.3.50:8111/api/tts/offline"; HttpPost httpPost = new H ...

  5. UIView的intrinsicContentSize方法,在按钮中重写

    场景,一个有图片有文字的按钮在其父控件上加了上左的约束,然后,有需要要调整按钮与图片之间的间距,第一想法就是设置title insets 的left. 然后问题出现了,这个按钮的文字居然显示不全了,也 ...

  6. [LeetCode] 303. Range Sum Query - Immutable 区域和检索 - 不可变

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

  7. [LeetCode] 375. Guess Number Higher or Lower II 猜数字大小 II

    We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gues ...

  8. IEDA中使用阿里插件Alibaba Cloud Toolkit和Arthas(阿尔萨斯)

    在 IntelliJ IDEA 中安装和配置 Cloud Toolkit 在 IntelliJ IDEA 中安装和配置 Cloud Toolkit 后,您可以将本地应用快速部署到阿里云 ECS.EDA ...

  9. ssh登录服务器提示错误no hostkey alg

    ssh登录服务器提示错误no hostkey alg ssh root@192.168.1.100 -vvv 提示失败: no hostkey alg 登录到192.168.1.100服务器 rm - ...

  10. kexue shangwang

    根据实践,pptp.IPsec甚至OpenVPN等kexue上网法已经无法顺利翻越GFW.通过抓包可知,GFW会将pptp的握手期间的ack包吞掉,导致本地一直无法收到服务器端的响应.而OpenVPN ...