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. cnetos7 搭建wordpress(apache+php+mariadb)

    .安装apache.php.php库 yum -y install httpd php php-mbstring php-pear 2.修改php配置文件地区 vim /etc/php.ini 在87 ...

  2. nodejs爬虫如何设置动态ip以及userAgent

    nodejs爬虫如何设置动态ip以及userAgent 转https://blog.csdn.net/u014374031/article/details/78833765 前言 在写nodejs爬虫 ...

  3. Python3基础 函数 函数名赋值操作

             Python : 3.7.3          OS : Ubuntu 18.04.2 LTS         IDE : pycharm-community-2019.1.3    ...

  4. Linux共享文件夹映射到Windows磁盘

    摘自:https://www.jianshu.com/p/4a06121450e5 1.1方案背景 本方案就是在解决磁盘不足的问题而产生的,利用映射盘的原理将Linux共享文件夹映射到Windows磁 ...

  5. dotfuscator 在混淆.Net Framework 4.0以上版本的时候报错的解决方法

    dotfuscator 在混淆.Net Framework 4.0以上版本的时候报错的解决方法 在混淆的时候报错了,错误描述大致如下: Could not find a compatible vers ...

  6. Javascript-基本使用

    本章向您提供了展示 JavaScript 能力的部分实例. JavaScript 能够改变 HTML 内容 getElementById() 是多个 JavaScript HTML 方法之一. 本例使 ...

  7. 多线程、线程池、线程创建、Thread

    转载自https://www.cnblogs.com/jmsjh/p/7762034.html 多线程 1.1 多线程介绍 学习多线程之前,我们先要了解几个关于多线程有关的概念. 进程:进程指正在运行 ...

  8. 下载youtube视频到本地

    https://www.clipconverter.cc/ 先通过上面的网站对youtube视频的url 进行解析获得下载链接地址 获得链接地址后 可通过阿里云香港服务器去下载 , 速度比较快 在阿里 ...

  9. win10系统svn传图片卡死

        win10系统svn传图片或者文件有时候会卡死,原因是此种文件的默认打开程序与svn冲突了 svn提交的时候要打开图片,但是图片默认打开程序也要打开 所以冲突了 改下不冲突的默认打开程序就行了 ...

  10. vue-cli3 每次打包都改变css img js文件名,还有自带版本号

    let Version = new Date().getTime(); css: { // 是否使用css分离插件 ExtractTextPlugin extract: { //一种方式,打包后的cs ...