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

  1. java归并排序,单线程vs多线程

    一.什么是归并排序 归并排序又称合并排序,它是成功应用分治技术的一个完美例子.对于一个需要排序的数组A[0..n-1],归并排序把它一分为二:A[0..n/2-1]和A[n/2..n-1],并对每个子 ...

  2. python单线程,多线程和协程速度对比

    在某些应用场景下,想要提高python的并发能力,可以使用多线程,或者协程.比如网络爬虫,数据库操作等一些IO密集型的操作.下面对比python单线程,多线程和协程在网络爬虫场景下的速度. 一,单线程 ...

  3. Python-爬取校花网视频(单线程和多线程版本)

    一.参考文章 python爬虫爬取校花网视频,单线程爬取 爬虫----爬取校花网视频,包含多线程版本 上述两篇文章都是对校花网视频的爬取,由于时间相隔很久了,校花网上的一些视频已经不存在了,因此上述文 ...

  4. Python-爬取妹子图(单线程和多线程版本)

    一.参考文章 Python爬虫之——爬取妹子图片 上述文章中的代码讲述的非常清楚,我的基本能思路也是这样,本篇文章中的代码仅仅做了一些异常处理和一些日志显示优化工作,写此文章主要是当做笔记,方便以后查 ...

  5. 面试之二:Redis是单线程还是多线程?以及处理模型。

      Redis是单线程还是多线程?以及处理模型. 线程:单线程 处理模型:参考书<Redis 设计与实现>P151-152 ![](https://ws1.sinaimg.cn/large ...

  6. zookeeper的c API 单线程与多线程问题 cli_st和cli_mt

    同样的程序,在centos和ubuntu上都没有问题,在solaris上问题却多多,据说是solaris管理更加严格. zookeeper_init方法,在传入一个错误的host也能初始化出一个非空的 ...

  7. Operating System-Thread(5)弹出式线程&&使单线程代码多线程化会产生那些问题

    本文主要内容 弹出式线程(Pop-up threads) 使单线程代码多线程化会产生那些问题 一.弹出式线程(Pop-up threads) 以在一个http到达之后一个Service的处理为例子来介 ...

  8. JS异步解决方案之概念理解-----------阻塞和非阻塞,同步和异步,并发和并行,单线程和多线程

    首先记住一句话,JS是单线程的. 单线程意味着什么?单线程意味着 它不能依靠自己实现异步. JS实现的异步,往往都是靠 浏览器.Node 的机制(事件驱动.回调)实现的. 下面让我这个单身狗 以谈恋爱 ...

  9. Spring Boot 定时任务单线程和多线程

    Spring Boot 的定时任务: 第一种:把参数配置到.properties文件中: 代码: package com.accord.task; import java.text.SimpleDat ...

随机推荐

  1. 案例(拖拽对话框、高清放大镜、自制滚动条、元素的隐藏方式、表格隔行变色、tab切换效果、字符串拼接、刷新评论)

    一.拖拽对话框 <style> .of{ width: 500px; } #link,#close{ text-decoration: none; margin: 0 10px; font ...

  2. CSPS分数取mod赛92-93

    我好菜啊..... 92只会打暴力,93暴力都不会了 模拟92, T1:直接ex_gcd加分类讨论即可 T2:考场只会打暴搜,正解为排序后线段树解决,排序的关键字为a+b,因为如果ai<bj&a ...

  3. centos7mongo集群

    1.安装 cat > /etc/yum.repos.d/mongodb.repo << EOF[mongodb-org-3.6]name=MongoDB Repositorybase ...

  4. nginx的跨域设置

    官方文档 中说,只有当响应状态码为以下几种类型中之一时,add_header 才会生效.如果需要 add_header 在所有情况下都生效,可以在后面加上 always 参数即可解决. Adds th ...

  5. 在学习ROS过程中碰到的一些问题--1

    好了,这是接触ROS的第三周了,初步了解了一下ROS,很多问题自己还是无法解决,但是想着很久没有在blog上记录自己的学习过程,就先胡乱写一下吧.^-^ 1.关于ROS各种基本概念的理解 这方面知识建 ...

  6. a=”hello”和b=”世界”编码成bytes类型

    a="hello" c=a.encode(encoding='utf-8') a = b'hello' b="世界" b = b.encode(encoding ...

  7. Educational Codeforces Round 49 (Rated for Div. 2)

    题目链接 还缺F和G,至少上橙之后把F补了吧. A - Palindromic Twist 题意:每个字母恰好操作一次,变成其之前或者其之后的一个字母,注意'a'和'z'不互通,求是否可以变成回文串. ...

  8. hadoop2.9.2 调整jvm

    错误:namenode挂掉 查看hadoop的日志文件,发现存在大量的GC,导致namenode挂掉 命令行执行错误信息: 解决: 查看系统内存: # /data1/hadoop/hadoop/etc ...

  9. Java 代码里乱打日志了,这才是正确的打日志姿势

    使用slf4j 使用门面模式的日志框架,有利于维护和各个类的日志处理方式统一. 实现方式统一使用: Logback框架 打日志的正确方式 什么时候应该打日志 当你遇到问题的时候,只能通过debug功能 ...

  10. C# - ZIP 压缩流

    C# - ZIP 压缩流 参考资料 https://docs.microsoft.com/en-us/dotnet/api/system.io.compression.ziparchive?view= ...