单线程与多线程的应用 --Python3
1、单线程应用
from time import ctime, sleep
from time import ctime, sleep
class ThreadClass:
def say(self):
print('Begin say:%s' % ctime())
sleep(2)
def write(self):
print('Brgin write:%s' % ctime())
sleep(2)
if __name__ == '__main__':
t = ThreadClass()
t.say()
t.write()
print('All end:%s' %ctime())
Begin say:Wed Jun 12 17:14:11 2019
Brgin write:Wed Jun 12 17:14:13 2019
All end:Wed Jun 12 17:14:15 2019
2、多线程的应用
from time import ctime, sleep
import threading
class ThreadClass:
def say(self, content, loop):
for i in range(loop):
print('Say:%s,Time:%s' % (content, ctime()))
sleep(2)
def write(self, content, loop):
for i in range(loop):
print('Write:%s,Time:%s' % (content, ctime()))
sleep(2)
if __name__ == '__main__':
threadings = []
tc = ThreadClass()
t1 = threading.Thread(target=tc.say, args=('say content', 2))
t2 = threading.Thread(target=tc.write, args=('write content', 2))
threadings.append(t1)
threadings.append(t2)
for t in threadings:
t.start()
for t in threadings:
t.join()
print('All the end:%s' % ctime())
Say:say content,Time:Wed Jun 12 21:58:31 2019
Write:write content,Time:Wed Jun 12 21:58:31 2019
Write:write content,Time:Wed Jun 12 21:58:33 2019
Say:say content,Time:Wed Jun 12 21:58:33 2019
All the end:Wed Jun 12 21:58:35 2019
3、扩展:多进程的应用
from time import ctime, sleep
import multiprocessing
class ThreadClass:
def say(self, content, loop):
for i in range(loop):
print('Say:%s,Time:%s' % (content, ctime()))
sleep(2)
def write(self, content, loop):
for i in range(loop):
print('Write:%s,Time:%s' % (content, ctime()))
sleep(2)
if __name__ == '__main__':
multiprocessings = []
tc = ThreadClass()
t1 = multiprocessing.Process(target=tc.say, args=('say content', 2))
t2 = multiprocessing.Process(target=tc.write, args=('write content', 2))
multiprocessings.append(t1)
multiprocessings.append(t2)
for t in multiprocessings:
t.start()
for t in multiprocessings:
t.join()
print('All the end:%s' % ctime())
Say:say content,Time:Wed Jun 12 22:03:54 2019
Write:write content,Time:Wed Jun 12 22:03:54 2019
Say:say content,Time:Wed Jun 12 22:03:56 2019
Write:write content,Time:Wed Jun 12 22:03:56 2019
All the end:Wed Jun 12 22:03:58 2019
单线程与多线程的应用 --Python3的更多相关文章
- java归并排序,单线程vs多线程
一.什么是归并排序 归并排序又称合并排序,它是成功应用分治技术的一个完美例子.对于一个需要排序的数组A[0..n-1],归并排序把它一分为二:A[0..n/2-1]和A[n/2..n-1],并对每个子 ...
- python单线程,多线程和协程速度对比
在某些应用场景下,想要提高python的并发能力,可以使用多线程,或者协程.比如网络爬虫,数据库操作等一些IO密集型的操作.下面对比python单线程,多线程和协程在网络爬虫场景下的速度. 一,单线程 ...
- Python-爬取校花网视频(单线程和多线程版本)
一.参考文章 python爬虫爬取校花网视频,单线程爬取 爬虫----爬取校花网视频,包含多线程版本 上述两篇文章都是对校花网视频的爬取,由于时间相隔很久了,校花网上的一些视频已经不存在了,因此上述文 ...
- Python-爬取妹子图(单线程和多线程版本)
一.参考文章 Python爬虫之——爬取妹子图片 上述文章中的代码讲述的非常清楚,我的基本能思路也是这样,本篇文章中的代码仅仅做了一些异常处理和一些日志显示优化工作,写此文章主要是当做笔记,方便以后查 ...
- 面试之二:Redis是单线程还是多线程?以及处理模型。
Redis是单线程还是多线程?以及处理模型. 线程:单线程 处理模型:参考书<Redis 设计与实现>P151-152 弹出式线程&&使单线程代码多线程化会产生那些问题
本文主要内容 弹出式线程(Pop-up threads) 使单线程代码多线程化会产生那些问题 一.弹出式线程(Pop-up threads) 以在一个http到达之后一个Service的处理为例子来介 ...
- JS异步解决方案之概念理解-----------阻塞和非阻塞,同步和异步,并发和并行,单线程和多线程
首先记住一句话,JS是单线程的. 单线程意味着什么?单线程意味着 它不能依靠自己实现异步. JS实现的异步,往往都是靠 浏览器.Node 的机制(事件驱动.回调)实现的. 下面让我这个单身狗 以谈恋爱 ...
- Spring Boot 定时任务单线程和多线程
Spring Boot 的定时任务: 第一种:把参数配置到.properties文件中: 代码: package com.accord.task; import java.text.SimpleDat ...
随机推荐
- AtCoder Beginner Contest 133
目录 Contest Info Solutions A. T or T B.Good Distance C. Remainder Minimization 2019 D. Rain Flows int ...
- tornado解析post数据的问题
解析tornado查询参数: self.request.query_arguments self.get_query_argument[s](参数名称) 解析tornado的post参数: self. ...
- Java面向对象6 (AA - AE)
整理音乐(SDUT 2053) import java.util.*; public class Main { public static void main(String[] args) { Sca ...
- Java for循环的语法和执行顺序
关于java的for循环想必大家非常熟悉,它是java常用的语句之一.for循环是最灵活也是最常用的循环结构,表达式一般如下: for(表达式1;表达式2;表达式4){ 表达式3; } 执行顺序: ...
- 在OpenFOAM中做用户自定义库——编译library【转载】
转载自:http://openfoam.blog.sohu.com/22041538.html OpenFOAM自己提供的标准类都是以库的形式提供的,并且利用头文件给出了库的应用接口.这样一来,用户的 ...
- Centos7 开启swap分区
阿里云购买的机器,默认不会开启swap分区,如有需要,需自行开启. 阿里当前的做法是: 1.不创建swap分区,由镜像决定 2.将vm.swappiness设定为0,即永不使用swap分区 开启swa ...
- 线程sleep方法的demo详解
sleep:超时等待指定时间,时间到了之后,重新回到就绪状态,抢到CPU资源后,立马进入运行状态: package com.roocon.thread.t1; public class NewThre ...
- IDEA在线和离线安装lombok
1. IDEA在线安装: 点击安装,电子reset 如果以上方式安装失败, 去以下任意网站下载对应版本插件安装: http://plugins.jetbrains.com/plugin/6317-l ...
- bagging,random forest,boosting(adaboost、GBDT),XGBoost小结
Bagging 从原始样本集中抽取训练集.每轮从原始样本集中使用Bootstraping(有放回)的方法抽取n个训练样本(在训练集中,有些样本可能被多次抽取到,而有些样本可能一次都没有被抽中).共进行 ...
- angular中子组件通过@Output 触发父组件的方 法
1. 子组件引入 Output 和 EventEmitter import { Component, OnInit ,Input,Output,EventEmitter} from '@angular ...