D10——C语言基础学PYTHON

时间:2022-11-24 06:01:35

C语言基础学习PYTHON——基础学习D10

20180906内容纲要:

  1、协程 

    (1)yield

    (2)greenlet

    (3)gevent

    (4)gevent实现单线程下socket多并发

  2、简单爬虫

  3、select

  4、IO多路复用

  5、小结

  6、练习

1 协程

协程
又叫微线程,纤程。协程是一种用户态的轻量级线程。
协程有自己的寄存器上下文和栈。协程调度切换时,将寄存器上下文、和栈保存到其他地方,在切换回来的时候回复先前保存的寄存器上下文和栈。
协程能保存上一次调用时的状态。一个cpu支持上万个协程都不是问题,很适合用于高并发处理。
协程的本质就是单线程。无法利用多核资源。

实现协程主要有以下几种方式:

(1)yield

 #Author:ZhangKanghui

 def Producer():
r = con1.__next__()
r = con2.__next__()
n = 0
while n < 5:
n += 1
con1.send(n)
con2.send(n)
print("\033[32;1m[Producer]\033[0m is making baozi %s"%n) def Consumer(name):
print("-->>start---")
while True:
new_balozi = yield
print("[%s] is eating baozi %s"%(name,new_balozi))
#time.sleep(1) if __name__ == '__main__':
con1 = Consumer("c1") #函数中存在yield只是生成器,并没有真正生成,所以需要necxt
con2 = Consumer("c2")
p = Producer()

yield实现协程

通过这个例子协程到底是什么呢?

  1. 必须在只有一个单线程里实现并发
  2. 修改共享数据不需加锁
  3. 用户程序里自己保存多个控制流的上下文栈
  4. 一个协程遇到IO操作自动切换到其它协程

当然,还有其他实现方式。

(2)greenlet

greenlet是一个用C实现的协程模块。

 #Author:ZhangKanghui

 from greenlet import greenlet
def func1():
print(12)
gr2.switch()
print(34)
gr2.switch() def func2():
print(56)
gr1.switch()
print(78) gr1 = greenlet(func1) #启动协程
gr2 = greenlet(func2)
gr1.switch()

greenlet实现协程

(3)gevent

Gevent 是一个第三方库,可以轻松通过gevent实现并发同步或异步编程。

在gevent中用到的主要模式是Greenlet, 它是以C扩展模块形式接入Python的轻量级协程。

Greenlet全部运行在主程序操作系统进程的内部,但它们被协作式地调度。

 #Author:ZhangKanghui

 import gevent

 def Foo():
print("Running in Foo")
gevent.sleep(2)
print("Explicit context switch to Foo again") def bar():
print("Explicit context to bar")
gevent.sleep(1)
print("Implicit context switch back to bar") def func():
print("running in func")
gevent.sleep(0)
print("running in func again")
gevent.joinall(
[gevent.spawn(Foo),
gevent.spawn(bar),
gevent.spawn(func)
])

gevent实现协程

(4)gevent实现单线程下socket多并发

 #Author:ZhangKanghui

 import sys
import socket
import time
import gevent
from gevent import socket,monkey monkey.patch_all() def server(port):
s = socket.socket()
s.bind(('localhost',port))
s.listen(500)
while True:
cli,addr = s.accept()
gevent.spawn(handle_request,cli) def handle_request(conn):
try:
while True:
data = conn.recv(10214)
print("recv:",data)
conn.send(data)
if not data:
conn.shutdown(socket.SHUT_WR)
except Exception as e:
print(e)
finally:
conn.close() if __name__ == '__main__':
server(8001)

gevnet_socket_server

 #Author:ZhangKanghui

 import socket

 HOST = 'localhost'
PORT = 8001
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect((HOST,PORT))
while True:
msg = bytes(input(">>:"),encoding="utf-8")
s.sendall(msg)
data = s.recv(1024) #print("Received",repr(data))
print("Received",data) s.close()

gevent_socket_client

2 简单爬虫

网络爬虫(又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常被称为网页追逐者),是一种按照一定的规则,自动的抓取万维网信息的程序或者脚本,已被广泛应用于互联网领域。搜索引擎使用网络爬虫抓取Web网页、文档甚至图片、音频、视频等资源,通过相应的索引技术组织这些信息,提供给搜索用户进行查询。网络爬虫也为中小站点的推广提供了有效的途径。

那么我们就简单来看看以下爬虫吧:

 #Author:ZhangKanghui

 from urllib import request

 def f(url):
print("GET:%s"%url)
resp = request.urlopen(url)
data = resp.read()
f = open("url.html",'wb')
f.write(data)
f.close()
print("%d bytes received from %s."%(len(data),url)) f("https://www.cnblogs.com/zhangkanghui/p/9577714.html")

爬虫urblib

 <!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="referrer" content="origin" />
<title>D09——C语言基础学PYTHON - m1racle - 博客园</title>
<meta property="og:description" content="C语言基础学习PYTHON——基础学习D09 20180903内容纲要: 线程、进程 1、paramiko 2、线程、进程初识 3、线程 (1)线程的调用方式 (2)join (3)线程锁、递归锁、信" />
<link type="text/css" rel="stylesheet" href="/bundles/blog-common.css?v=D7Le-lOZiZVAXQkZQuNwdTWqjabXaVBE_2YAWzY_YZs1"/>
<link id="MainCss" type="text/css" rel="stylesheet" href="/skins/red_autumnal_leaves/bundle-red_autumnal_leaves.css?v=EDEp2d1uMe8iyN6qDoW8MQgYb-JCFIeiYP0oX3XiiRM1"/>
<link id="mobile-style" media="only screen and (max-width: 767px)" type="text/css" rel="stylesheet" href="/skins/red_autumnal_leaves/bundle-red_autumnal_leaves-mobile.css?v=d9LctKHRIQp9rreugMcQ1-UJuq_j1fo0GZXTXj8Bqrk1"/>
<link title="RSS" type="application/rss+xml" rel="alternate" href="https://www.cnblogs.com/zhangkanghui/rss"/>
<link title="RSD" type="application/rsd+xml" rel="EditURI" href="https://www.cnblogs.com/zhangkanghui/rsd.xml"/>
<link type="application/wlwmanifest+xml" rel="wlwmanifest" href="https://www.cnblogs.com/zhangkanghui/wlwmanifest.xml"/>
<script src="//common.cnblogs.com/scripts/jquery-2.2.0.min.js"></script>
<script type="text/javascript">var currentBlogApp = 'zhangkanghui', cb_enable_mathjax=false;var isLogined=false;</script>
<script src="/bundles/blog-common.js?v=yRkjgN2sBQkB4hX-wirHxPomeBT9sB5dawr6ob7KIvg1" type="text/javascript"></script>
</head>
<body>
<a name="top"></a> <!--done-->
<div id="home">
<div id="header">
<div id="blogTitle">
<a id="lnkBlogLogo" href="https://www.cnblogs.com/zhangkanghui/"><img id="blogLogo" src="/Skins/custom/images/logo.gif" alt="返回主页" /></a> <!--done-->
<h1><a id="Header1_HeaderTitle" class="headermaintitle" href="https://www.cnblogs.com/zhangkanghui/">m1racle</a></h1>
<h2></h2> </div><!--end: blogTitle 博客的标题和副标题 -->
<div id="navigator"> <ul id="navList">
<li><a id="blog_nav_sitehome" class="menu" href="https://www.cnblogs.com/">博客园</a></li>
<li><a id="blog_nav_myhome" class="menu" href="https://www.cnblogs.com/zhangkanghui/">首页</a></li>
<li><a id="blog_nav_newpost" class="menu" rel="nofollow" href="https://i.cnblogs.com/EditPosts.aspx?opt=1">新随笔</a></li>
<li><a id="blog_nav_contact" class="menu" rel="nofollow" href="https://msg.cnblogs.com/send/m1racle">联系</a></li>
<li><a id="blog_nav_rss" class="menu" href="https://www.cnblogs.com/zhangkanghui/rss">订阅</a>
<!--<a id="blog_nav_rss_image" class="aHeaderXML" href="https://www.cnblogs.com/zhangkanghui/rss"><img src="//www.cnblogs.com/images/xml.gif" alt="订阅" /></a>--></li>
<li><a id="blog_nav_admin" class="menu" rel="nofollow" href="https://i.cnblogs.com/">管理</a></li>
</ul>
<div class="blogStats"> <div id="blog_stats">
<span id="stats_post_count">随笔 - 8&nbsp; </span>
<span id="stats_article_count">文章 - 0&nbsp; </span>
<span id="stats-comment_count">评论 - 1</span>
</div> </div><!--end: blogStats -->
</div><!--end: navigator 博客导航栏 -->
</div><!--end: header 头部 --> <div id="main">
<div id="mainContent">
<div class="forFlow"> <div id="post_detail">
<!--done-->
<div id="topics">
<div class = "post">
<h1 class = "postTitle">
<a id="cb_post_title_url" class="postTitle2" href="https://www.cnblogs.com/zhangkanghui/p/9577714.html">D09——C语言基础学PYTHON</a>
</h1>
<div class="clear"></div>
<div class="postBody">
<div id="cnblogs_post_body" class="blogpost-body"><p><span style="font-size: 18pt; background-color: #ffff00"><strong>C语言基础学习PYTHON——基础学习D09</strong></span></p>
<blockquote>
<p><strong><span style="font-size: 18px">20180903内容纲要:</span></strong></p>
</blockquote>
<p>&nbsp;</p>
<p><strong><strong>   线程、进程</strong></strong></p>
<p>&nbsp;</p>
<p><strong><strong>  1、paramiko</strong></strong></p>
<p><strong><strong>  2、线程、进程初识</strong></strong></p>
<p><strong><strong>  3、线程</strong></strong></p>
<p><strong><strong>     (1)线程的调用方式</strong></strong></p>
<p><strong><strong>     (2)join</strong></strong></p>
<p><strong><strong>     (3)线程锁、递归锁、信号量</strong></strong></p>
<p><strong>     (4)Timer</strong></p>
<p><strong>     (5)Event</strong></p>
<p><strong>     (6)Queue队列</strong></p>
<p><strong>  4、小结</strong></p>
<p><strong>  5、练习:简单主机批量管理工具</strong></p>
<p><strong>&nbsp;</strong></p>
<blockquote>
<p><span style="font-size: 14pt"><strong>1、paramiko</strong></span></p>
</blockquote>
<p>paramiko模块提供了ssh及sft进行远程登录服务器执行命令和上传下载文件的功能。这是一个第三方的软件包,使用之前需要安装。</p>
<p>我个人觉得,这个在Windows上不太好用。在windows上python3需要安装vs2010,但我装了之后还是不行,可能是我自己的原因,在python2.7上好像可以,没试过。</p>
<p>这有链接:<a href="https://blog.csdn.net/songfreeman/article/details/50920767" target="_blank">https://blog.csdn.net/songfreeman/article/details/50920767<br></a></p>
<p>很详细!</p>
<p>&nbsp;</p>
<blockquote>
<p><span style="font-size: 14pt"><strong>2 线程、进程初识</strong></span></p> </blockquote>
<h3>什么是线程(thread)?</h3>
<p>线程是操作系统能够进行运算调度的最小单位。它被包含在进程之中,是进程中的实际运作单位。</p>
<p>一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行执行不同的任务</p>
<p>A thread is an execution context, which is all the information a CPU needs to execute a stream of instructions.</p>
<div class="cnblogs_code"><img id="code_img_closed_f52f3cbd-a865-4c8a-bd82-2732e6fb8022" class="code_img_closed" src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" alt=""><img id="code_img_opened_f52f3cbd-a865-4c8a-bd82-2732e6fb8022" class="code_img_opened" style="display: none" src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" alt="">
<div id="cnblogs_code_open_f52f3cbd-a865-4c8a-bd82-2732e6fb8022" class="cnblogs_code_hide">
<pre>Suppose you<span style="color: #800000">'</span><span style="color: #800000">re reading a book, and you want to take a break right now, but you want to be able to come back and resume reading from the exact point where you stopped. One way to achieve that is by jotting down the page number, line number, and word number. So your execution context for reading a book is these 3 numbers.</span>
<span style="color: #000000">
If you have a roommate, </span><span style="color: #0000ff">and</span> she<span style="color: #800000">'</span><span style="color: #800000">s using the same technique, she can take the book while you</span><span style="color: #800000">'</span>re <span style="color: #0000ff">not</span> using it, <span style="color: #0000ff">and</span> resume reading <span style="color: #0000ff">from</span> where she stopped. Then you can take it back, <span style="color: #0000ff">and</span> resume it <span style="color: #0000ff">from</span><span style="color: #000000"> where you were. Threads work </span><span style="color: #0000ff">in</span> the same way. A CPU <span style="color: #0000ff">is</span> giving you the illusion that it<span style="color: #800000">'</span><span style="color: #800000">s doing multiple computations at the same time. It does that by spending a bit of time on each computation. It can do that because it has an execution context for each computation. Just like you can share a book with your friend, many tasks can share a CPU.</span>
<span style="color: #000000">
On a more technical level, an execution context (therefore a thread) consists of the values of the CPU</span><span style="color: #800000">'</span><span style="color: #800000">s registers.</span>
<span style="color: #000000">
Last: threads are different </span><span style="color: #0000ff">from</span> processes. A thread <span style="color: #0000ff">is</span> a context of execution, <span style="color: #0000ff">while</span> a process <span style="color: #0000ff">is</span> a bunch of resources associated with a computation. A process can have one <span style="color: #0000ff">or</span><span style="color: #000000"> many threads. Clarification: the resources associated with a process include memory pages (all the threads </span><span style="color: #0000ff">in</span> a process have the same view of the memory), file descriptors (e.g., open sockets), <span style="color: #0000ff">and</span> security credentials (e.g., the ID of the user who started the process).</pre>
</div>
<span class="cnblogs_code_collapse">英文解释</span></div>
<h3>什么是进程(process)?</h3>
<p>程序并不能单独运行,只有将程序装载到内存中,系统为它分配资源才能运行,而这种执行的程序就称之为进程。</p>
<p>程序和进程的区别就在于:程序是指令的集合,它是进程运行的静态描述文本;进程是程序的一次执行活动,属于动态概念。</p>
<p>An executing instance of a program is called a process.</p>
<div class="cnblogs_code"><img id="code_img_closed_a81bb69d-34be-4e5b-86b5-d9787eee621a" class="code_img_closed" src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" alt=""><img id="code_img_opened_a81bb69d-34be-4e5b-86b5-d9787eee621a" class="code_img_opened" style="display: none" src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" alt="">
<div id="cnblogs_code_open_a81bb69d-34be-4e5b-86b5-d9787eee621a" class="cnblogs_code_hide">
<pre>Each process provides the resources needed to execute a program. A process has a virtual address space, executable code, open handles to system objects, a security context, a unique process identifier, environment variables, a priority <span style="color: #0000ff">class</span>, minimum <span style="color: #0000ff">and</span> maximum working set sizes, <span style="color: #0000ff">and</span> at least one thread of execution. Each process <span style="color: #0000ff">is</span> started with a single thread, often called the primary thread, but can create additional threads <span style="color: #0000ff">from</span> any of its threads.</pre>
</div>
<span class="cnblogs_code_collapse">英文解释</span></div>
<p>&nbsp;</p>
<p>那么他们的区别呢?</p>
<h3>进程与线程的区别?</h3>
<p>线程:是操作系统最小的调度单位。 是一串指令的集合。</p>
<p>进程:要操作cpu必须先创建一个线程</p>
<p>所有在同一个进程里的线程是共享一块内存空间</p>
<p>&nbsp;</p>
<div class="cnblogs_code"><img id="code_img_closed_70388922-c789-45c9-a798-77fd21c98ac2" class="code_img_closed" src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" alt=""><img id="code_img_opened_70388922-c789-45c9-a798-77fd21c98ac2" class="code_img_opened" style="display: none" src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" alt="">
<div id="cnblogs_code_open_70388922-c789-45c9-a798-77fd21c98ac2" class="cnblogs_code_hide">
<pre><span style="color: #008080"> 1</span> 1<span style="color: #000000">、Threads share the address space of the process that created it; processes have their own address space.
</span><span style="color: #008080"> 2</span> 1<span style="color: #000000">、线程共享内存空间,进程的内存是独立的
</span><span style="color: #008080"> 3</span>
<span style="color: #008080"> 4</span> 2<span style="color: #000000">、Threads have direct access to the data segment of its process; processes have their own copy of the data segment of the parent process.
</span><span style="color: #008080"> 5</span>
<span style="color: #008080"> 6</span> 3<span style="color: #000000">、Threads can directly communicate with other threads of its process; processes must use interprocess communication to communicate with sibling processes.
</span><span style="color: #008080"> 7</span> 3<span style="color: #000000">、两个进程之间可以直接交流,两个进程进行通讯,必须通过一个中间代理来实现。
</span><span style="color: #008080"> 8</span>
<span style="color: #008080"> 9</span> 4<span style="color: #000000">、New threads are easily created; new processes require duplication of the parent process.
</span><span style="color: #008080">10</span> 4<span style="color: #000000">、创建新线程很简单。创建新进程需要对其父进程进行一次克隆
</span><span style="color: #008080">11</span>
<span style="color: #008080">12</span> 5<span style="color: #000000">、Threads can exercise considerable control over threads of the same process; processes can only exercise control over child processes.
</span><span style="color: #008080">13</span> 5<span style="color: #000000">、一个线程可以控制和操作同意进程里的其他线程,但是进程只能操作子进程
</span><span style="color: #008080">14</span>
<span style="color: #008080">15</span> 6、Changes to the main thread (cancellation, priority change, etc.) may affect the behavior of the other threads of the process; changes to the parent process does <span style="color: #0000ff">not</span><span style="color: #000000"> affect child processes.
</span><span style="color: #008080">16</span> </pre>
</div>
<span class="cnblogs_code_collapse">进程与线程的区别</span></div>
<p>&nbsp;</p>
<blockquote>
<p><span style="font-size: 14pt"><strong>3 线程</strong></span></p>
</blockquote>
<p><span style="font-size: 14px"><strong>(1)线程的调用方式</strong></span></p>
<p><span style="font-size: 14px">直接调用:</span></p>
<div class="cnblogs_code"><img id="code_img_closed_8cc59f33-5a03-49bd-b99c-591a20547e10" class="code_img_closed" src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" alt=""><img id="code_img_opened_8cc59f33-5a03-49bd-b99c-591a20547e10" class="code_img_opened" style="display: none" src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" alt="">
<div id="cnblogs_code_open_8cc59f33-5a03-49bd-b99c-591a20547e10" class="cnblogs_code_hide">
<pre><span style="color: #008080"> 1</span> <span style="color: #008000">#</span><span style="color: #008000">Author:ZhangKanghui</span>
<span style="color: #008080"> 2</span>
<span style="color: #008080"> 3</span> <span style="color: #0000ff">import</span><span style="color: #000000"> threading
</span><span style="color: #008080"> 4</span> <span style="color: #0000ff">import</span><span style="color: #000000"> time
</span><span style="color: #008080"> 5</span>
<span style="color: #008080"> 6</span> <span style="color: #0000ff">def</span><span style="color: #000000"> run(n):
</span><span style="color: #008080"> 7</span> <span style="color: #0000ff">print</span>(<span style="color: #800000">"</span><span style="color: #800000">task</span><span style="color: #800000">"</span><span style="color: #000000">,n,threading.current_thread())
</span><span style="color: #008080"> 8</span> time.sleep(2<span style="color: #000000">)
</span><span style="color: #008080"> 9</span> <span style="color: #800000">'''</span><span style="color: #800000">#这里是只有两个线程,那么更多线程呢?
</span><span style="color: #008080">10</span> <span style="color: #800000">t1 = threading.Thread(target=run,args=("t1",))
</span><span style="color: #008080">11</span> <span style="color: #800000">t2 = threading.Thread(target=run,args=("t2",))
</span><span style="color: #008080">12</span> <span style="color: #800000">t1.start()
</span><span style="color: #008080">13</span> <span style="color: #800000">t2.start()
</span><span style="color: #008080">14</span> <span style="color: #800000">'''</span>
<span style="color: #008080">15</span> <span style="color: #008000">#</span><span style="color: #008000">这是一个多线程,那么我们来看一下他整个程序的执行时间为什么不是2s</span>
<span style="color: #008080">16</span> start_time =<span style="color: #000000"> time.time()
</span><span style="color: #008080">17</span> <span style="color: #0000ff">for</span> i <span style="color: #0000ff">in</span> range(50<span style="color: #000000">):
</span><span style="color: #008080">18</span> t = threading.Thread(target=run,args=(<span style="color: #800000">"</span><span style="color: #800000">t-%s</span><span style="color: #800000">"</span> %i,)) <span style="color: #008000">#</span><span style="color: #008000">这个地方args必须有,。。因为这是默认一个元组</span>
<span style="color: #008080">19</span> <span style="color: #000000"> t.start()
</span><span style="color: #008080">20</span> <span style="color: #008000">#</span><span style="color: #008000">t.join() #这样可以把并行变成串行</span>
<span style="color: #008080">21</span>
<span style="color: #008080">22</span> <span style="color: #0000ff">print</span>(<span style="color: #800000">"</span><span style="color: #800000">mian threading has finished....</span><span style="color: #800000">"</span><span style="color: #000000">,threading.current_thread(),threading.active_count())
</span><span style="color: #008080">23</span> <span style="color: #0000ff">print</span>(<span style="color: #800000">"</span><span style="color: #800000">cost:</span><span style="color: #800000">"</span>,time.time()-<span style="color: #000000">start_time)
</span><span style="color: #008080">24</span> <span style="color: #008000">#</span><span style="color: #008000">这是因为主线程和子线程没关系。主线程不会等子线程执行完毕才计算时间</span>
<span style="color: #008080">25</span> <span style="color: #008000">#</span><span style="color: #008000">那接下来如果想要等所有的线程结束然后打印程序执行时间可以这么干</span>
<span style="color: #008080">26</span> <span style="color: #800000">'''</span>
<span style="color: #008080">27</span>
<span style="color: #008080">28</span> <span style="color: #800000">import threading
</span><span style="color: #008080">29</span> <span style="color: #800000">import time
</span><span style="color: #008080">30</span>
<span style="color: #008080">31</span> <span style="color: #800000">def run(n):
</span><span style="color: #008080">32</span> <span style="color: #800000"> print("task",n)
</span><span style="color: #008080">33</span> <span style="color: #800000"> time.sleep(2)
</span><span style="color: #008080">34</span>
<span style="color: #008080">35</span>
<span style="color: #008080">36</span> <span style="color: #800000">start_time = time.time()
</span><span style="color: #008080">37</span> <span style="color: #800000">t_obj =[] #存线程实例
</span><span style="color: #008080">38</span> <span style="color: #800000">for i in range(50):
</span><span style="color: #008080">39</span> <span style="color: #800000"> t = threading.Thread(target=run,args=("t-%s" %i,)) #这个地方args必须有,。。因为这是默认一个元组
</span><span style="color: #008080">40</span> <span style="color: #800000"> #t.setDaemon(True) #把当前线程设置成守护线程,必须在启动strat之前
</span><span style="color: #008080">41</span> <span style="color: #800000"> t.start()
</span><span style="color: #008080">42</span> <span style="color: #800000"> t_obj.append(t) #为了不阻塞后面线程的启动,不在这里加join,先放到一个列表里
</span><span style="color: #008080">43</span> <span style="color: #800000">for i in t_obj: #循环线程实例列表,等待所有线程执行完毕
</span><span style="color: #008080">44</span> <span style="color: #800000"> t.join()
</span><span style="color: #008080">45</span>
<span style="color: #008080">46</span> <span style="color: #800000">print("cost:",time.time()-start_time)
</span><span style="color: #008080">47</span>
<span style="color: #008080">48</span> <span style="color: #800000">'''</span></pre>
</div>
<span class="cnblogs_code_collapse">直接调用</span></div>
<p>继承式调用</p>
<div class="cnblogs_code"><img id="code_img_closed_9cc95b50-0dd2-4780-98f6-e19ce4f49bd9" class="code_img_closed" src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" alt=""><img id="code_img_opened_9cc95b50-0dd2-4780-98f6-e19ce4f49bd9" class="code_img_opened" style="display: none" src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" alt="">
<div id="cnblogs_code_open_9cc95b50-0dd2-4780-98f6-e19ce4f49bd9" class="cnblogs_code_hide">
<pre><span style="color: #008080"> 1</span> <span style="color: #008000">#</span><span style="color: #008000">Author:ZhangKanghui</span>
<span style="color: #008080"> 2</span>
<span style="color: #008080"> 3</span> <span style="color: #0000ff">import</span><span style="color: #000000"> threading
</span><span style="color: #008080"> 4</span> <span style="color: #0000ff">import</span><span style="color: #000000"> time
</span><span style="color: #008080"> 5</span>
<span style="color: #008080"> 6</span> <span style="color: #0000ff">class</span><span style="color: #000000"> MyThread(threading.Thread):
</span><span style="color: #008080"> 7</span> <span style="color: #0000ff">def</span> <span style="color: #800080">__init__</span><span style="color: #000000">(self,n,sleep_time):
</span><span style="color: #008080"> 8</span> super(MyThread,self).<span style="color: #800080">__init__</span><span style="color: #000000">()
</span><span style="color: #008080"> 9</span> self.n =<span style="color: #000000"> n
</span><span style="color: #008080">10</span> self.sleep_time =<span style="color: #000000"> sleep_time
</span><span style="color: #008080">11</span>
<span style="color: #008080">12</span> <span style="color: #0000ff">def</span><span style="color: #000000"> run(self):
</span><span style="color: #008080">13</span> <span style="color: #0000ff">print</span>(<span style="color: #800000">"</span><span style="color: #800000">running task</span><span style="color: #800000">"</span><span style="color: #000000">,self.n)
</span><span style="color: #008080">14</span> <span style="color: #000000"> time.sleep(self.sleep_time)
</span><span style="color: #008080">15</span> <span style="color: #0000ff">print</span>(<span style="color: #800000">"</span><span style="color: #800000">task done</span><span style="color: #800000">"</span><span style="color: #000000">,self.n)
</span><span style="color: #008080">16</span>
<span style="color: #008080">17</span> t1 =MyThread(<span style="color: #800000">"</span><span style="color: #800000">t1</span><span style="color: #800000">"</span>,2<span style="color: #000000">)
</span><span style="color: #008080">18</span> t2 =MyThread(<span style="color: #800000">"</span><span style="color: #800000">t2</span><span style="color: #800000">"</span>,4<span style="color: #000000">)
</span><span style="color: #008080">19</span>
<span style="color: #008080">20</span> <span style="color: #000000">t1.start()
</span><span style="color: #008080">21</span> <span style="color: #000000">t2.start()
</span><span style="color: #008080">22</span>
<span style="color: #008080">23</span> <span style="color: #008000">#</span><span style="color: #008000">t1.join()</span>
<span style="color: #008080">24</span> <span style="color: #008000">#</span><span style="color: #008000">t2.join()</span>
<span style="color: #008080">25</span>
<span style="color: #008080">26</span> <span style="color: #0000ff">print</span>(<span style="color: #800000">"</span><span style="color: #800000">mian threading....</span><span style="color: #800000">"</span>)</pre>
</div>
<span class="cnblogs_code_collapse">继承式调用</span></div>
<p><strong>(2)join&amp;Daemon</strong></p>
<p>其实join就是wait。Daemon就是守护线程。</p>
<p>Some threads do background tasks, like sending keepalive packets, or performing periodic garbage collection, or whatever. These are only useful when the main program is running, and it's okay to kill them off once the other, non-daemon, threads have exited.</p>
<p>Without daemon threads, you'd have to keep track of them, and tell them to exit, before your program can completely quit. By setting them as daemon threads, you can let them run and forget about them, and when your program quits, any daemon threads are killed automatically.</p>
<div class="cnblogs_code"><img id="code_img_closed_2deaebbd-3a16-45bf-ae48-b77a3abc102d" class="code_img_closed" src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" alt=""><img id="code_img_opened_2deaebbd-3a16-45bf-ae48-b77a3abc102d" class="code_img_opened" style="display: none" src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" alt="">
<div id="cnblogs_code_open_2deaebbd-3a16-45bf-ae48-b77a3abc102d" class="cnblogs_code_hide">
<pre><span style="color: #008080"> 1</span> <span style="color: #008000">#</span><span style="color: #008000">Author:ZhangKanghui</span>
<span style="color: #008080"> 2</span> <span style="color: #0000ff">import</span><span style="color: #000000"> time
</span><span style="color: #008080"> 3</span> <span style="color: #0000ff">import</span><span style="color: #000000"> threading
</span><span style="color: #008080"> 4</span>
<span style="color: #008080"> 5</span>
<span style="color: #008080"> 6</span> <span style="color: #0000ff">def</span><span style="color: #000000"> run(n):
</span><span style="color: #008080"> 7</span> <span style="color: #0000ff">print</span>(<span style="color: #800000">'</span><span style="color: #800000">[%s]------running----\n</span><span style="color: #800000">'</span> %<span style="color: #000000"> n)
</span><span style="color: #008080"> 8</span> time.sleep(2<span style="color: #000000">)
</span><span style="color: #008080"> 9</span> <span style="color: #0000ff">print</span>(<span style="color: #800000">'</span><span style="color: #800000">--done--</span><span style="color: #800000">'</span><span style="color: #000000">)
</span><span style="color: #008080">10</span>
<span style="color: #008080">11</span>
<span style="color: #008080">12</span> <span style="color: #0000ff">def</span><span style="color: #000000"> main():
</span><span style="color: #008080">13</span> <span style="color: #0000ff">for</span> i <span style="color: #0000ff">in</span> range(5<span style="color: #000000">):
</span><span style="color: #008080">14</span> t = threading.Thread(target=run, args=<span style="color: #000000">[i, ])
</span><span style="color: #008080">15</span> <span style="color: #000000"> t.start()
</span><span style="color: #008080">16</span> t.join(1<span style="color: #000000">)
</span><span style="color: #008080">17</span> <span style="color: #0000ff">print</span>(<span style="color: #800000">'</span><span style="color: #800000">starting thread</span><span style="color: #800000">'</span><span style="color: #000000">, t.getName())
</span><span style="color: #008080">18</span>
<span style="color: #008080">19</span>
<span style="color: #008080">20</span> m = threading.Thread(target=main, args=<span style="color: #000000">[])
</span><span style="color: #008080">21</span> m.setDaemon(True) <span style="color: #008000">#</span><span style="color: #008000"> 将main线程设置为Daemon线程,它做为程序主线程的守护线程,当主线程退出时,m线程也会退出,由m启动的其它子线程会同时退出,不管是否执行完任务</span>
<span style="color: #008080">22</span> <span style="color: #000000">m.start()
</span><span style="color: #008080">23</span> <span style="color: #000000">m.join()
</span><span style="color: #008080">24</span> <span style="color: #008000">#</span><span style="color: #008000"> m.join(timeout=2)</span>
<span style="color: #008080">25</span> <span style="color: #0000ff">print</span>(<span style="color: #800000">"</span><span style="color: #800000">---main thread done----</span><span style="color: #800000">"</span>)</pre>
</div>
<span class="cnblogs_code_collapse">join&amp;Daemon</span></div>
<div class="cnblogs_code"><img id="code_img_closed_d1c17aec-1b1a-487c-ab9c-638d96b8f0a9" class="code_img_closed" src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" alt=""><img id="code_img_opened_d1c17aec-1b1a-487c-ab9c-638d96b8f0a9" class="code_img_opened" style="display: none" src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" alt="">
<div id="cnblogs_code_open_d1c17aec-1b1a-487c-ab9c-638d96b8f0a9" class="cnblogs_code_hide">
<pre><span style="color: #008080"> 1</span> <span style="color: #008000">#</span><span style="color: #008000">Author:ZhangKanghui</span>
<span style="color: #008080"> 2</span>
<span style="color: #008080"> 3</span> <span style="color: #0000ff">import</span><span style="color: #000000"> threading
</span><span style="color: #008080"> 4</span> <span style="color: #0000ff">import</span><span style="color: #000000"> time
</span><span style="color: #008080"> 5</span>
<span style="color: #008080"> 6</span> <span style="color: #0000ff">class</span><span style="color: #000000"> MyThread(threading.Thread):
</span><span style="color: #008080"> 7</span> <span style="color: #0000ff">def</span> <span style="color: #800080">__init__</span><span style="color: #000000">(self,n,sleep_time):
</span><span style="color: #008080"> 8</span> super(MyThread,self).<span style="color: #800080">__init__</span><span style="color: #000000">()
</span><span style="color: #008080"> 9</span> self.n =<span style="color: #000000"> n
</span><span style="color: #008080">10</span> self.sleep_time =<span style="color: #000000"> sleep_time
</span><span style="color: #008080">11</span>
<span style="color: #008080">12</span> <span style="color: #0000ff">def</span><span style="color: #000000"> run(self):
</span><span style="color: #008080">13</span> <span style="color: #0000ff">print</span>(<span style="color: #800000">"</span><span style="color: #800000">running task</span><span style="color: #800000">"</span><span style="color: #000000">,self.n)
</span><span style="color: #008080">14</span> <span style="color: #000000"> time.sleep(self.sleep_time)
</span><span style="color: #008080">15</span> <span style="color: #0000ff">print</span>(<span style="color: #800000">"</span><span style="color: #800000">task done</span><span style="color: #800000">"</span><span style="color: #000000">,self.n)
</span><span style="color: #008080">16</span>
<span style="color: #008080">17</span> t1 =MyThread(<span style="color: #800000">"</span><span style="color: #800000">t1</span><span style="color: #800000">"</span>,2<span style="color: #000000">)
</span><span style="color: #008080">18</span> t2 =MyThread(<span style="color: #800000">"</span><span style="color: #800000">t2</span><span style="color: #800000">"</span>,4<span style="color: #000000">)
</span><span style="color: #008080">19</span>
<span style="color: #008080">20</span> <span style="color: #000000">t1.start()
</span><span style="color: #008080">21</span> <span style="color: #000000">t2.start()
</span><span style="color: #008080">22</span>
<span style="color: #008080">23</span> <span style="color: #000000">t1.join()
</span><span style="color: #008080">24</span> <span style="color: #000000">t2.join()
</span><span style="color: #008080">25</span>
<span style="color: #008080">26</span> <span style="color: #0000ff">print</span>(<span style="color: #800000">"</span><span style="color: #800000">mian threading....</span><span style="color: #800000">"</span>)</pre>
</div>
<span class="cnblogs_code_collapse">join1</span></div>
<div class="cnblogs_code"><img id="code_img_closed_14a3cdfa-7082-4d31-a48d-3fdae50438a9" class="code_img_closed" src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" alt=""><img id="code_img_opened_14a3cdfa-7082-4d31-a48d-3fdae50438a9" class="code_img_opened" style="display: none" src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" alt="">
<div id="cnblogs_code_open_14a3cdfa-7082-4d31-a48d-3fdae50438a9" class="cnblogs_code_hide">
<pre><span style="color: #008080"> 1</span> <span style="color: #0000ff">import</span><span style="color: #000000"> threading
</span><span style="color: #008080"> 2</span> <span style="color: #0000ff">import</span><span style="color: #000000"> time
</span><span style="color: #008080"> 3</span>
<span style="color: #008080"> 4</span> <span style="color: #0000ff">def</span><span style="color: #000000"> run(n):
</span><span style="color: #008080"> 5</span> <span style="color: #0000ff">print</span>(<span style="color: #800000">"</span><span style="color: #800000">task</span><span style="color: #800000">"</span><span style="color: #000000">,n)
</span><span style="color: #008080"> 6</span> time.sleep(2<span style="color: #000000">)
</span><span style="color: #008080"> 7</span>
<span style="color: #008080"> 8</span> start_time =<span style="color: #000000"> time.time()
</span><span style="color: #008080"> 9</span> t_obj =[] <span style="color: #008000">#</span><span style="color: #008000">存线程实例</span>
<span style="color: #008080">10</span> <span style="color: #0000ff">for</span> i <span style="color: #0000ff">in</span> range(50<span style="color: #000000">):
</span><span style="color: #008080">11</span> t = threading.Thread(target=run,args=(<span style="color: #800000">"</span><span style="color: #800000">t-%s</span><span style="color: #800000">"</span> %i,)) <span style="color: #008000">#</span><span style="color: #008000">这个地方args必须有,。。因为这是默认一个元组</span>
<span style="color: #008080">12</span> <span style="color: #008000">#</span><span style="color: #008000">t.setDaemon(True) #把当前线程设置成守护线程,必须在启动strat之前</span>
<span style="color: #008080">13</span> <span style="color: #000000"> t.start()
</span><span style="color: #008080">14</span> t_obj.append(t) <span style="color: #008000">#</span><span style="color: #008000">为了不阻塞后面线程的启动,不在这里加join,先放到一个列表里</span>
<span style="color: #008080">15</span> <span style="color: #0000ff">for</span> i <span style="color: #0000ff">in</span> t_obj: <span style="color: #008000">#</span><span style="color: #008000">循环线程实例列表,等待所有线程执行完毕</span>
<span style="color: #008080">16</span> <span style="color: #000000"> t.join()
</span><span style="color: #008080">17</span>
<span style="color: #008080">18</span> <span style="color: #0000ff">print</span>(<span style="color: #800000">"</span><span style="color: #800000">cost:</span><span style="color: #800000">"</span>,time.time()-start_time)</pre>
</div>
<span class="cnblogs_code_collapse">join2</span></div>
<p><strong>(3)线程锁/递归锁/信号量</strong></p>
<p><strong>线程锁</strong></p>
<p>一个进程下可以启动多个线程,多个线程共享父进程的内存空间,也就意味着每个线程可以访问同一份数据,此时,如果2个线程同时要修改同一份数据,会出现什么状况?</p>
<div class="cnblogs_code"><img id="code_img_closed_37379dfa-a22b-422a-b0d0-ea12dc9490c0" class="code_img_closed" src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" alt=""><img id="code_img_opened_37379dfa-a22b-422a-b0d0-ea12dc9490c0" class="code_img_opened" style="display: none" src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" alt="">
<div id="cnblogs_code_open_37379dfa-a22b-422a-b0d0-ea12dc9490c0" class="cnblogs_code_hide">
<pre><span style="color: #008080"> 1</span> <span style="color: #0000ff">import</span><span style="color: #000000"> time
</span><span style="color: #008080"> 2</span> <span style="color: #0000ff">import</span><span style="color: #000000"> threading
</span><span style="color: #008080"> 3</span>
<span style="color: #008080"> 4</span> <span style="color: #0000ff">def</span><span style="color: #000000"> addNum():
</span><span style="color: #008080"> 5</span> <span style="color: #0000ff">global</span> num <span style="color: #008000">#</span><span style="color: #008000">在每个线程中都获取这个全局变量</span>
<span style="color: #008080"> 6</span> <span style="color: #0000ff">print</span>(<span style="color: #800000">'</span><span style="color: #800000">--get num:</span><span style="color: #800000">'</span><span style="color: #000000">,num )
</span><span style="color: #008080"> 7</span> time.sleep(1<span style="color: #000000">)
</span><span style="color: #008080"> 8</span> lock.acquire() <span style="color: #008000">#</span><span style="color: #008000">修改数据前加锁</span>
<span style="color: #008080"> 9</span> num -=1 <span style="color: #008000">#</span><span style="color: #008000">对此公共变量进行-1操作</span>
<span style="color: #008080">10</span> lock.release() <span style="color: #008000">#</span><span style="color: #008000">修改后释放</span>
<span style="color: #008080">11</span>
<span style="color: #008080">12</span> num = 100 <span style="color: #008000">#</span><span style="color: #008000">设定一个共享变量</span>
<span style="color: #008080">13</span> thread_list =<span style="color: #000000"> []
</span><span style="color: #008080">14</span> lock = threading.Lock() <span style="color: #008000">#</span><span style="color: #008000">生成全局锁</span>
<span style="color: #008080">15</span> <span style="color: #0000ff">for</span> i <span style="color: #0000ff">in</span> range(100<span style="color: #000000">):
</span><span style="color: #008080">16</span> t = threading.Thread(target=<span style="color: #000000">addNum)
</span><span style="color: #008080">17</span> <span style="color: #000000"> t.start()
</span><span style="color: #008080">18</span> <span style="color: #000000"> thread_list.append(t)
</span><span style="color: #008080">19</span>
<span style="color: #008080">20</span> <span style="color: #0000ff">for</span> t <span style="color: #0000ff">in</span> thread_list: <span style="color: #008000">#</span><span style="color: #008000">等待所有线程执行完毕</span>
<span style="color: #008080">21</span> <span style="color: #000000"> t.join()
</span><span style="color: #008080">22</span>
<span style="color: #008080">23</span> <span style="color: #0000ff">print</span>(<span style="color: #800000">'</span><span style="color: #800000">final num:</span><span style="color: #800000">'</span>, num )</pre>
</div>
<span class="cnblogs_code_collapse">线程锁</span></div>
<p><strong>递归锁</strong></p>
<p>就是在一个大锁中还要再包含子锁</p>
<div class="cnblogs_code"><img id="code_img_closed_8dbacbd9-7372-40b1-9ea6-8418bdd0e6da" class="code_img_closed" src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" alt=""><img id="code_img_opened_8dbacbd9-7372-40b1-9ea6-8418bdd0e6da" class="code_img_opened" style="display: none" src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" alt="">
<div id="cnblogs_code_open_8dbacbd9-7372-40b1-9ea6-8418bdd0e6da" class="cnblogs_code_hide">
<pre><span style="color: #008080"> 1</span> <span style="color: #0000ff">import</span><span style="color: #000000"> threading,time
</span><span style="color: #008080"> 2</span>
<span style="color: #008080"> 3</span> <span style="color: #0000ff">def</span><span style="color: #000000"> run1():
</span><span style="color: #008080"> 4</span> <span style="color: #0000ff">print</span>(<span style="color: #800000">"</span><span style="color: #800000">grab the first part data</span><span style="color: #800000">"</span><span style="color: #000000">)
</span><span style="color: #008080"> 5</span> <span style="color: #000000"> lock.acquire()
</span><span style="color: #008080"> 6</span> <span style="color: #0000ff">global</span><span style="color: #000000"> num
</span><span style="color: #008080"> 7</span> num +=1
<span style="color: #008080"> 8</span> <span style="color: #000000"> lock.release()
</span><span style="color: #008080"> 9</span> <span style="color: #0000ff">return</span><span style="color: #000000"> num
</span><span style="color: #008080">10</span> <span style="color: #0000ff">def</span><span style="color: #000000"> run2():
</span><span style="color: #008080">11</span> <span style="color: #0000ff">print</span>(<span style="color: #800000">"</span><span style="color: #800000">grab the second part data</span><span style="color: #800000">"</span><span style="color: #000000">)
</span><span style="color: #008080">12</span> <span style="color: #000000"> lock.acquire()
</span><span style="color: #008080">13</span> <span style="color: #0000ff">global</span><span style="color: #000000"> num2
</span><span style="color: #008080">14</span> num2+=1
<span style="color: #008080">15</span> <span style="color: #000000"> lock.release()
</span><span style="color: #008080">16</span> <span style="color: #0000ff">return</span><span style="color: #000000"> num2
</span><span style="color: #008080">17</span> <span style="color: #0000ff">def</span><span style="color: #000000"> run3():
</span><span style="color: #008080">18</span> <span style="color: #000000"> lock.acquire()
</span><span style="color: #008080">19</span> res =<span style="color: #000000"> run1()
</span><span style="color: #008080">20</span> <span style="color: #0000ff">print</span>(<span style="color: #800000">'</span><span style="color: #800000">--------between run1 and run2-----</span><span style="color: #800000">'</span><span style="color: #000000">)
</span><span style="color: #008080">21</span> res2 =<span style="color: #000000"> run2()
</span><span style="color: #008080">22</span> <span style="color: #000000"> lock.release()
</span><span style="color: #008080">23</span> <span style="color: #0000ff">print</span><span style="color: #000000">(res,res2)
</span><span style="color: #008080">24</span>
<span style="color: #008080">25</span>
<span style="color: #008080">26</span> <span style="color: #0000ff">if</span> <span style="color: #800080">__name__</span> == <span style="color: #800000">'</span><span style="color: #800000">__main__</span><span style="color: #800000">'</span><span style="color: #000000">:
</span><span style="color: #008080">27</span>
<span style="color: #008080">28</span> num,num2 =<span style="color: #000000"> 0,0
</span><span style="color: #008080">29</span> lock =<span style="color: #000000"> threading.RLock()
</span><span style="color: #008080">30</span> <span style="color: #0000ff">for</span> i <span style="color: #0000ff">in</span> range(10<span style="color: #000000">):
</span><span style="color: #008080">31</span> t = threading.Thread(target=<span style="color: #000000">run3)
</span><span style="color: #008080">32</span> <span style="color: #000000"> t.start()
</span><span style="color: #008080">33</span>
<span style="color: #008080">34</span> <span style="color: #0000ff">while</span> threading.active_count() != 1<span style="color: #000000">:
</span><span style="color: #008080">35</span> <span style="color: #0000ff">print</span><span style="color: #000000">(threading.active_count())
</span><span style="color: #008080">36</span> <span style="color: #0000ff">else</span><span style="color: #000000">:
</span><span style="color: #008080">37</span> <span style="color: #0000ff">print</span>(<span style="color: #800000">'</span><span style="color: #800000">----all threads done---</span><span style="color: #800000">'</span><span style="color: #000000">)
</span><span style="color: #008080">38</span> <span style="color: #0000ff">print</span>(num,num2)</pre>
</div>
<span class="cnblogs_code_collapse">递归锁</span></div>
<p><strong>信号量(Semaphore)</strong></p>
<p>互斥锁 同时只允许一个线程更改数据,而Semaphore是同时允许一定数量的线程更改数据 。</p>
<p>比如厕所有3个坑,那最多只允许3个人上厕所,后面的人只能等里面有人出来了才能再进去。</p>
<div class="cnblogs_code"><img id="code_img_closed_77906072-186c-4497-8adf-816d6a972222" class="code_img_closed" src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" alt=""><img id="code_img_opened_77906072-186c-4497-8adf-816d6a972222" class="code_img_opened" style="display: none" src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" alt="">
<div id="cnblogs_code_open_77906072-186c-4497-8adf-816d6a972222" class="cnblogs_code_hide">
<pre><span style="color: #008080"> 1</span> <span style="color: #0000ff">import</span><span style="color: #000000"> threading,time
</span><span style="color: #008080"> 2</span>
<span style="color: #008080"> 3</span> <span style="color: #0000ff">def</span><span style="color: #000000"> run(n):
</span><span style="color: #008080"> 4</span> <span style="color: #000000"> semaphore.acquire()
</span><span style="color: #008080"> 5</span> time.sleep(1<span style="color: #000000">)
</span><span style="color: #008080"> 6</span> <span style="color: #0000ff">print</span>(<span style="color: #800000">"</span><span style="color: #800000">run the thread: %s\n</span><span style="color: #800000">"</span> %<span style="color: #000000">n)
</span><span style="color: #008080"> 7</span> <span style="color: #000000"> semaphore.release()
</span><span style="color: #008080"> 8</span>
<span style="color: #008080"> 9</span> <span style="color: #0000ff">if</span> <span style="color: #800080">__name__</span> == <span style="color: #800000">'</span><span style="color: #800000">__main__</span><span style="color: #800000">'</span><span style="color: #000000">:
</span><span style="color: #008080">10</span>
<span style="color: #008080">11</span> num=<span style="color: #000000"> 0
</span><span style="color: #008080">12</span> semaphore = threading.BoundedSemaphore(5) <span style="color: #008000">#</span><span style="color: #008000">最多允许5个线程同时运行</span>
<span style="color: #008080">13</span> <span style="color: #0000ff">for</span> i <span style="color: #0000ff">in</span> range(20<span style="color: #000000">):
</span><span style="color: #008080">14</span> t = threading.Thread(target=run,args=<span style="color: #000000">(i,))
</span><span style="color: #008080">15</span> <span style="color: #000000"> t.start()
</span><span style="color: #008080">16</span>
<span style="color: #008080">17</span> <span style="color: #0000ff">while</span> threading.active_count() != 1<span style="color: #000000">:
</span><span style="color: #008080">18</span> <span style="color: #0000ff">pass</span> <span style="color: #008000">#</span><span style="color: #008000">print threading.active_count()</span>
<span style="color: #008080">19</span> <span style="color: #0000ff">else</span><span style="color: #000000">:
</span><span style="color: #008080">20</span> <span style="color: #0000ff">print</span>(<span style="color: #800000">'</span><span style="color: #800000">----all threads done---</span><span style="color: #800000">'</span><span style="color: #000000">)
</span><span style="color: #008080">21</span> <span style="color: #0000ff">print</span>(num)</pre>
</div>
<span class="cnblogs_code_collapse">信号量</span></div>
<p><strong>(4)Timer</strong></p>
<p>This class represents an action that should be run only after a certain amount of time has passed。</p>
<div class="cnblogs_code"><img id="code_img_closed_5decea23-4aca-4e15-82a1-14d039aeb20e" class="code_img_closed" src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" alt=""><img id="code_img_opened_5decea23-4aca-4e15-82a1-14d039aeb20e" class="code_img_opened" style="display: none" src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" alt="">
<div id="cnblogs_code_open_5decea23-4aca-4e15-82a1-14d039aeb20e" class="cnblogs_code_hide">
<pre><span style="color: #008080">1</span> <span style="color: #008000">#</span><span style="color: #008000">Author:ZhangKanghui</span>
<span style="color: #008080">2</span> <span style="color: #0000ff">import</span><span style="color: #000000"> threading
</span><span style="color: #008080">3</span> <span style="color: #0000ff">def</span><span style="color: #000000"> hello():
</span><span style="color: #008080">4</span> <span style="color: #0000ff">print</span>(<span style="color: #800000">"</span><span style="color: #800000">hello, world</span><span style="color: #800000">"</span><span style="color: #000000">)
</span><span style="color: #008080">5</span>
<span style="color: #008080">6</span> t = threading.Thread(target=<span style="color: #000000">hello)
</span><span style="color: #008080">7</span> t = threading.Timer(30.0<span style="color: #000000">, hello)
</span><span style="color: #008080">8</span> t.start() <span style="color: #008000">#</span><span style="color: #008000"> after 30 seconds, "hello, world" will be printed</span></pre>
</div>
<span class="cnblogs_code_collapse">Timer</span></div>
<p><strong>(5)Event</strong></p>
<p>An event is a simple synchronization object;通过Event来实现两个或多个线程间的交互。</p>
<p>the event represents an internal flag, and threads&nbsp;can wait for the flag to be set, or set or clear the flag themselves.</p>
<p>event.wait()</p>
<p>event.set()</p>
<p>event.clear()</p>
<p>下面是一个红绿灯的例子,即起动一个线程做交通指挥灯,生成几个线程做车辆,车辆行驶按红灯停,绿灯行的规则。</p>
<div class="cnblogs_code"><img id="code_img_closed_bc894591-a7dd-4526-b4d1-1e92981f2a96" class="code_img_closed" src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" alt=""><img id="code_img_opened_bc894591-a7dd-4526-b4d1-1e92981f2a96" class="code_img_opened" style="display: none" src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" alt="">
<div id="cnblogs_code_open_bc894591-a7dd-4526-b4d1-1e92981f2a96" class="cnblogs_code_hide">
<pre><span style="color: #008080"> 1</span> <span style="color: #0000ff">import</span><span style="color: #000000"> threading,time
</span><span style="color: #008080"> 2</span> <span style="color: #0000ff">import</span><span style="color: #000000"> random
</span><span style="color: #008080"> 3</span> <span style="color: #0000ff">def</span><span style="color: #000000"> light():
</span><span style="color: #008080"> 4</span> <span style="color: #0000ff">if</span> <span style="color: #0000ff">not</span><span style="color: #000000"> event.isSet():
</span><span style="color: #008080"> 5</span> event.set() <span style="color: #008000">#</span><span style="color: #008000">wait就不阻塞 #绿灯状态</span>
<span style="color: #008080"> 6</span> count =<span style="color: #000000"> 0
</span><span style="color: #008080"> 7</span> <span style="color: #0000ff">while</span><span style="color: #000000"> True:
</span><span style="color: #008080"> 8</span> <span style="color: #0000ff">if</span> count &lt; 10<span style="color: #000000">:
</span><span style="color: #008080"> 9</span> <span style="color: #0000ff">print</span>(<span style="color: #800000">'</span><span style="color: #800000">\033[42;1m--green light on---\033[0m</span><span style="color: #800000">'</span><span style="color: #000000">)
</span><span style="color: #008080">10</span> <span style="color: #0000ff">elif</span> count &lt;13<span style="color: #000000">:
</span><span style="color: #008080">11</span> <span style="color: #0000ff">print</span>(<span style="color: #800000">'</span><span style="color: #800000">\033[43;1m--yellow light on---\033[0m</span><span style="color: #800000">'</span><span style="color: #000000">)
</span><span style="color: #008080">12</span> <span style="color: #0000ff">elif</span> count &lt;20<span style="color: #000000">:
</span><span style="color: #008080">13</span> <span style="color: #0000ff">if</span><span style="color: #000000"> event.isSet():
</span><span style="color: #008080">14</span> <span style="color: #000000"> event.clear()
</span><span style="color: #008080">15</span> <span style="color: #0000ff">print</span>(<span style="color: #800000">'</span><span style="color: #800000">\033[41;1m--red light on---\033[0m</span><span style="color: #800000">'</span><span style="color: #000000">)
</span><span style="color: #008080">16</span> <span style="color: #0000ff">else</span><span style="color: #000000">:
</span><span style="color: #008080">17</span> count =<span style="color: #000000"> 0
</span><span style="color: #008080">18</span> event.set() <span style="color: #008000">#</span><span style="color: #008000">打开绿灯</span>
<span style="color: #008080">19</span> time.sleep(1<span style="color: #000000">)
</span><span style="color: #008080">20</span> count +=1
<span style="color: #008080">21</span> <span style="color: #0000ff">def</span><span style="color: #000000"> car(n):
</span><span style="color: #008080">22</span> <span style="color: #0000ff">while</span> 1<span style="color: #000000">:
</span><span style="color: #008080">23</span> time.sleep(random.randrange(10<span style="color: #000000">))
</span><span style="color: #008080">24</span> <span style="color: #0000ff">if</span> event.isSet(): <span style="color: #008000">#</span><span style="color: #008000">绿灯</span>
<span style="color: #008080">25</span> <span style="color: #0000ff">print</span>(<span style="color: #800000">"</span><span style="color: #800000">car [%s] is running..</span><span style="color: #800000">"</span> %<span style="color: #000000"> n)
</span><span style="color: #008080">26</span> <span style="color: #0000ff">else</span><span style="color: #000000">:
</span><span style="color: #008080">27</span> <span style="color: #0000ff">print</span>(<span style="color: #800000">"</span><span style="color: #800000">car [%s] is waiting for the red light..</span><span style="color: #800000">"</span> %<span style="color: #000000">n)
</span><span style="color: #008080">28</span> <span style="color: #0000ff">if</span> <span style="color: #800080">__name__</span> == <span style="color: #800000">'</span><span style="color: #800000">__main__</span><span style="color: #800000">'</span><span style="color: #000000">:
</span><span style="color: #008080">29</span> event =<span style="color: #000000"> threading.Event()
</span><span style="color: #008080">30</span> Light = threading.Thread(target=<span style="color: #000000">light)
</span><span style="color: #008080">31</span> <span style="color: #000000"> Light.start()
</span><span style="color: #008080">32</span> <span style="color: #0000ff">for</span> i <span style="color: #0000ff">in</span> range(3<span style="color: #000000">):
</span><span style="color: #008080">33</span> t = threading.Thread(target=car,args=<span style="color: #000000">(i,))
</span><span style="color: #008080">34</span> t.start()</pre>
</div>
<span class="cnblogs_code_collapse">Event实例之红绿灯</span></div>
<p>这里还有一个event使用的例子,员工进公司门要刷卡, 我们这里设置一个线程是“门”, 再设置几个线程为“员工”,员工看到门没打开,就刷卡,刷完卡,门开了,员工就可以通过。</p>
<div class="cnblogs_code"><img id="code_img_closed_8096f152-6a5c-4c8c-8b74-3a926e5b33c5" class="code_img_closed" src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" alt=""><img id="code_img_opened_8096f152-6a5c-4c8c-8b74-3a926e5b33c5" class="code_img_opened" style="display: none" src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" alt="">
<div id="cnblogs_code_open_8096f152-6a5c-4c8c-8b74-3a926e5b33c5" class="cnblogs_code_hide">
<pre><span style="color: #008080"> 1</span> <span style="color: #008000">#</span><span style="color: #008000">_*_coding:utf-8_*_</span>
<span style="color: #008080"> 2</span> <span style="color: #800080">__author__</span> = <span style="color: #800000">'</span><span style="color: #800000">Alex Li</span><span style="color: #800000">'</span>
<span style="color: #008080"> 3</span> <span style="color: #0000ff">import</span><span style="color: #000000"> threading
</span><span style="color: #008080"> 4</span> <span style="color: #0000ff">import</span><span style="color: #000000"> time
</span><span style="color: #008080"> 5</span> <span style="color: #0000ff">import</span><span style="color: #000000"> random
</span><span style="color: #008080"> 6</span>
<span style="color: #008080"> 7</span> <span style="color: #0000ff">def</span><span style="color: #000000"> door():
</span><span style="color: #008080"> 8</span> door_open_time_counter =<span style="color: #000000"> 0
</span><span style="color: #008080"> 9</span> <span style="color: #0000ff">while</span><span style="color: #000000"> True:
</span><span style="color: #008080">10</span> <span style="color: #0000ff">if</span><span style="color: #000000"> door_swiping_event.is_set():
</span><span style="color: #008080">11</span> <span style="color: #0000ff">print</span>(<span style="color: #800000">"</span><span style="color: #800000">\033[32;1mdoor opening....\033[0m</span><span style="color: #800000">"</span><span style="color: #000000">)
</span><span style="color: #008080">12</span> door_open_time_counter +=1
<span style="color: #008080">13</span>
<span style="color: #008080">14</span> <span style="color: #0000ff">else</span><span style="color: #000000">:
</span><span style="color: #008080">15</span> <span style="color: #0000ff">print</span>(<span style="color: #800000">"</span><span style="color: #800000">\033[31;1mdoor closed...., swipe to open.\033[0m</span><span style="color: #800000">"</span><span style="color: #000000">)
</span><span style="color: #008080">16</span> door_open_time_counter = 0 <span style="color: #008000">#</span><span style="color: #008000">清空计时器</span>
<span style="color: #008080">17</span> <span style="color: #000000"> door_swiping_event.wait()
</span><span style="color: #008080">18</span>
<span style="color: #008080">19</span>
<span style="color: #008080">20</span> <span style="color: #0000ff">if</span> door_open_time_counter &gt; 3:<span style="color: #008000">#</span><span style="color: #008000">门开了已经3s了,该关了</span>
<span style="color: #008080">21</span> <span style="color: #000000"> door_swiping_event.clear()
</span><span style="color: #008080">22</span>
<span style="color: #008080">23</span> time.sleep(0.5<span style="color: #000000">)
</span><span style="color: #008080">24</span>
<span style="color: #008080">25</span>
<span style="color: #008080">26</span> <span style="color: #0000ff">def</span><span style="color: #000000"> staff(n):
</span><span style="color: #008080">27</span>
<span style="color: #008080">28</span> <span style="color: #0000ff">print</span>(<span style="color: #800000">"</span><span style="color: #800000">staff [%s] is comming...</span><span style="color: #800000">"</span> %<span style="color: #000000"> n )
</span><span style="color: #008080">29</span> <span style="color: #0000ff">while</span><span style="color: #000000"> True:
</span><span style="color: #008080">30</span> <span style="color: #0000ff">if</span><span style="color: #000000"> door_swiping_event.is_set():
</span><span style="color: #008080">31</span> <span style="color: #0000ff">print</span>(<span style="color: #800000">"</span><span style="color: #800000">\033[34;1mdoor is opened, passing.....\033[0m</span><span style="color: #800000">"</span><span style="color: #000000">)
</span><span style="color: #008080">32</span> <span style="color: #0000ff">break</span>
<span style="color: #008080">33</span> <span style="color: #0000ff">else</span><span style="color: #000000">:
</span><span style="color: #008080">34</span> <span style="color: #0000ff">print</span>(<span style="color: #800000">"</span><span style="color: #800000">staff [%s] sees door got closed, swipping the card.....</span><span style="color: #800000">"</span> %<span style="color: #000000"> n)
</span><span style="color: #008080">35</span> <span style="color: #0000ff">print</span><span style="color: #000000">(door_swiping_event.set())
</span><span style="color: #008080">36</span> <span style="color: #000000"> door_swiping_event.set()
</span><span style="color: #008080">37</span> <span style="color: #0000ff">print</span>(<span style="color: #800000">"</span><span style="color: #800000">after set </span><span style="color: #800000">"</span><span style="color: #000000">,door_swiping_event.set())
</span><span style="color: #008080">38</span> time.sleep(0.5<span style="color: #000000">)
</span><span style="color: #008080">39</span> door_swiping_event = threading.Event() <span style="color: #008000">#</span><span style="color: #008000">设置事件</span>
<span style="color: #008080">40</span>
<span style="color: #008080">41</span>
<span style="color: #008080">42</span> door_thread = threading.Thread(target=<span style="color: #000000">door)
</span><span style="color: #008080">43</span> <span style="color: #000000">door_thread.start()
</span><span style="color: #008080">44</span>
<span style="color: #008080">45</span>
<span style="color: #008080">46</span>
<span style="color: #008080">47</span> <span style="color: #0000ff">for</span> i <span style="color: #0000ff">in</span> range(5<span style="color: #000000">):
</span><span style="color: #008080">48</span> p = threading.Thread(target=staff,args=<span style="color: #000000">(i,))
</span><span style="color: #008080">49</span> time.sleep(random.randrange(3<span style="color: #000000">))
</span><span style="color: #008080">50</span> p.start()</pre>
</div>
<span class="cnblogs_code_collapse">event实例之刷卡</span></div>
<p>&nbsp;</p>
<p><span style="font-size: 14px"><strong>&nbsp;(6)queue队列</strong></span></p>
<p><span style="font-size: 14px">queue is especially useful in threaded programming when information must be exchanged safely between multiple threads.</span></p>
<p>&nbsp;</p>
<dl class="class"><dt id="queue.Queue"><strong>class&nbsp;<code class="descclassname">queue.</code><code class="descname">Queue</code><span class="sig-paren">(maxsize=0<span class="sig-paren">) #先入先出</span></span></strong></dt><dt><strong>class&nbsp;<code class="descclassname">queue.</code><code class="descname">LifoQueue</code><span class="sig-paren">(maxsize=0<span class="sig-paren">) #last in fisrt out&nbsp;</span></span></strong></dt><dt><strong>class&nbsp;<code class="descclassname">queue.</code><code class="descname">PriorityQueue</code><span class="sig-paren">(maxsize=0<span class="sig-paren">) #存储数据时可设置优先级的队列</span></span></strong></dt><dt></dt><dt></dt></dl><dl class="method"><dt id="queue.Queue.qsize"><code class="descclassname">Queue.</code><code class="descname">qsize</code><span class="sig-paren">(<span class="sig-paren">)</span></span></dt><dt><code class="descclassname">Queue.</code><code class="descname">empty</code><span class="sig-paren">(<span class="sig-paren">) #return True if empty &nbsp;</span></span></dt><dt><code class="descclassname">Queue.</code><code class="descname">full</code><span class="sig-paren">(<span class="sig-paren">) # return True if full&nbsp;</span></span></dt><dt><span class="sig-paren"><span class="sig-paren"><code class="descclassname">Queue.</code><code class="descname">get</code><span class="sig-paren">(<em>block=True</em>,&nbsp;<em>timeout=None</em><span class="sig-paren">)</span></span></span></span></dt><dt><code class="descclassname">Queue.</code><code class="descname">put</code><span class="sig-paren">(<em>item</em>,&nbsp;<em>block=True</em>,&nbsp;<em>timeout=None</em><span class="sig-paren">)</span></span></dt><dt><span class="sig-paren"><span class="sig-paren"><code class="descclassname">Queue.</code><code class="descname">put_nowait</code><span class="sig-paren">(<em>item</em><span class="sig-paren">)</span></span></span></span></dt><dt></dt><dt><code class="descclassname">Queue.</code><code class="descname">get_nowait</code><span class="sig-paren">(<span class="sig-paren">)</span></span></dt><dt><span class="sig-paren"><code class="descclassname">Queue.</code><code class="descname">join</code><span class="sig-paren">(<span class="sig-paren">) block直到queue被消费完毕</span></span></span></dt><dt></dt><dt></dt><dt></dt></dl>
<p>&nbsp;a、先入先出</p>
<div class="cnblogs_code"><img id="code_img_closed_9c0b729a-9405-4711-8ecd-5c8043b4dc52" class="code_img_closed" src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" alt=""><img id="code_img_opened_9c0b729a-9405-4711-8ecd-5c8043b4dc52" class="code_img_opened" style="display: none" src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" alt="">
<div id="cnblogs_code_open_9c0b729a-9405-4711-8ecd-5c8043b4dc52" class="cnblogs_code_hide">
<pre><span style="color: #008080"> 1</span> <span style="color: #008000">#</span><span style="color: #008000">Author:ZhangKanghui</span>
<span style="color: #008080"> 2</span>
<span style="color: #008080"> 3</span> <span style="color: #0000ff">import</span><span style="color: #000000"> queue
</span><span style="color: #008080"> 4</span> q =<span style="color: #000000">queue.Queue()
</span><span style="color: #008080"> 5</span>
<span style="color: #008080"> 6</span> q.put(1<span style="color: #000000">)
</span><span style="color: #008080"> 7</span> q.put(2<span style="color: #000000">)
</span><span style="color: #008080"> 8</span> q.put(3<span style="color: #000000">)
</span><span style="color: #008080"> 9</span>
<span style="color: #008080">10</span> <span style="color: #0000ff">print</span><span style="color: #000000">(q.qsize())
</span><span style="color: #008080">11</span> <span style="color: #0000ff">print</span><span style="color: #000000">(q.get())
</span><span style="color: #008080">12</span> <span style="color: #0000ff">print</span><span style="color: #000000">(q.get())
</span><span style="color: #008080">13</span> <span style="color: #0000ff">print</span><span style="color: #000000">(q.get())
</span><span style="color: #008080">14</span> <span style="color: #008000">#</span><span style="color: #008000">先入先出,这个时候如果在获取呢?就会挂起卡死</span>
<span style="color: #008080">15</span> <span style="color: #0000ff">print</span><span style="color: #000000">(q.get())
</span><span style="color: #008080">16</span> <span style="color: #008000">#</span><span style="color: #008000">在这里看不出来什么效果。去命令行试试</span></pre>
</div>
<span class="cnblogs_code_collapse">先入先出</span></div>
<p><img src="https://images2018.cnblogs.com/blog/1435523/201809/1435523-20180903141516230-1658960823.png" alt=""></p>
<p>存在一个问题就是,当把所有的数据都get到之后,程序就会挂起卡死。那这个时候怎么办呢?</p>
<p>q.get_nowait() &nbsp;或者通过get(block=True,Timeout)参数进行设置</p>
<p>会出现先一个queue Empty的异常,这个时候可以通过捕获异常使程序正常进行。</p>
<p><img src="https://images2018.cnblogs.com/blog/1435523/201809/1435523-20180903141732769-380682019.png" alt=""></p>
<p><img src="https://images2018.cnblogs.com/blog/1435523/201809/1435523-20180903142118901-1628911451.png" alt=""></p>
<p><img src="https://images2018.cnblogs.com/blog/1435523/201809/1435523-20180903142153407-430380383.png" alt=""></p>
<p>&nbsp;</p>
<p>b、后进先出</p>
<div class="cnblogs_code"><img id="code_img_closed_57672cd1-85fa-4509-a13d-781b91ae2351" class="code_img_closed" src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" alt=""><img id="code_img_opened_57672cd1-85fa-4509-a13d-781b91ae2351" class="code_img_opened" style="display: none" src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" alt="">
<div id="cnblogs_code_open_57672cd1-85fa-4509-a13d-781b91ae2351" class="cnblogs_code_hide">
<pre><span style="color: #008080"> 1</span> <span style="color: #008000">#</span><span style="color: #008000">Author:ZhangKanghui</span>
<span style="color: #008080"> 2</span>
<span style="color: #008080"> 3</span> <span style="color: #0000ff">import</span><span style="color: #000000"> queue
</span><span style="color: #008080"> 4</span> q =<span style="color: #000000">queue.LifoQueue()
</span><span style="color: #008080"> 5</span> q.put(1<span style="color: #000000">)
</span><span style="color: #008080"> 6</span> q.put(2<span style="color: #000000">)
</span><span style="color: #008080"> 7</span> q.put(3<span style="color: #000000">)
</span><span style="color: #008080"> 8</span>
<span style="color: #008080"> 9</span> <span style="color: #0000ff">print</span><span style="color: #000000">(q.qsize())
</span><span style="color: #008080">10</span> <span style="color: #0000ff">print</span><span style="color: #000000">(q.get())
</span><span style="color: #008080">11</span> <span style="color: #0000ff">print</span><span style="color: #000000">(q.get())
</span><span style="color: #008080">12</span> <span style="color: #0000ff">print</span>(q.get())</pre>
</div>
<span class="cnblogs_code_collapse">后进先出LifoQueue</span></div>
<p>c、设置优先级</p>
<div class="cnblogs_code"><img id="code_img_closed_d61f3381-175d-4842-82ab-e947705523a2" class="code_img_closed" src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" alt=""><img id="code_img_opened_d61f3381-175d-4842-82ab-e947705523a2" class="code_img_opened" style="display: none" src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" alt="">
<div id="cnblogs_code_open_d61f3381-175d-4842-82ab-e947705523a2" class="cnblogs_code_hide">
<pre><span style="color: #008080"> 1</span> <span style="color: #008000">#</span><span style="color: #008000">Author:ZhangKanghui</span>
<span style="color: #008080"> 2</span>
<span style="color: #008080"> 3</span> <span style="color: #0000ff">import</span><span style="color: #000000"> queue
</span><span style="color: #008080"> 4</span>
<span style="color: #008080"> 5</span> q =<span style="color: #000000">queue.PriorityQueue()
</span><span style="color: #008080"> 6</span> q.put((1,<span style="color: #800000">"</span><span style="color: #800000">erha</span><span style="color: #800000">"</span><span style="color: #000000">))
</span><span style="color: #008080"> 7</span> q.put((5,<span style="color: #800000">"</span><span style="color: #800000">hashiqi</span><span style="color: #800000">"</span><span style="color: #000000">))
</span><span style="color: #008080"> 8</span> q.put((3,<span style="color: #800000">"</span><span style="color: #800000">taidi</span><span style="color: #800000">"</span><span style="color: #000000">))
</span><span style="color: #008080"> 9</span> <span style="color: #0000ff">print</span><span style="color: #000000">(q.get())
</span><span style="color: #008080">10</span> <span style="color: #0000ff">print</span><span style="color: #000000">(q.get())
</span><span style="color: #008080">11</span> <span style="color: #0000ff">print</span>(q.get())</pre>
</div>
<span class="cnblogs_code_collapse">优先级队列</span></div>
<p>&nbsp;</p>
<p>下面以一个生产者消费者的模型实例来深刻体会一下队列。</p>
<div class="cnblogs_code"><img id="code_img_closed_b57c3665-dcb9-4f96-aa10-61b107d20eb2" class="code_img_closed" src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" alt=""><img id="code_img_opened_b57c3665-dcb9-4f96-aa10-61b107d20eb2" class="code_img_opened" style="display: none" src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" alt="">
<div id="cnblogs_code_open_b57c3665-dcb9-4f96-aa10-61b107d20eb2" class="cnblogs_code_hide">
<pre><span style="color: #0000ff">import</span><span style="color: #000000"> threading
</span><span style="color: #0000ff">import</span><span style="color: #000000"> queue </span><span style="color: #0000ff">def</span><span style="color: #000000"> producer():
</span><span style="color: #0000ff">for</span> i <span style="color: #0000ff">in</span> range(10<span style="color: #000000">):
q.put(</span><span style="color: #800000">"</span><span style="color: #800000">baozi %s</span><span style="color: #800000">"</span> %<span style="color: #000000"> i ) </span><span style="color: #0000ff">print</span>(<span style="color: #800000">"</span><span style="color: #800000">开始等待所有的包子被取走...</span><span style="color: #800000">"</span><span style="color: #000000">)
q.join()
</span><span style="color: #0000ff">print</span>(<span style="color: #800000">"</span><span style="color: #800000">所有的包子被取完了...</span><span style="color: #800000">"</span><span style="color: #000000">) </span><span style="color: #0000ff">def</span><span style="color: #000000"> consumer(n): </span><span style="color: #0000ff">while</span> q.qsize() &gt;<span style="color: #000000">0: </span><span style="color: #0000ff">print</span>(<span style="color: #800000">"</span><span style="color: #800000">%s 取到</span><span style="color: #800000">"</span> %<span style="color: #000000">n , q.get())
q.task_done() </span><span style="color: #008000">#</span><span style="color: #008000">告知这个任务执行完了</span>
<span style="color: #000000"> q </span>=<span style="color: #000000"> queue.Queue() p </span>= threading.Thread(target=<span style="color: #000000">producer,)
p.start() c1 </span>= consumer(<span style="color: #800000">"</span><span style="color: #800000">二哈</span><span style="color: #800000">"</span>)</pre>
</div>
<span class="cnblogs_code_collapse">生产者消费者模型1</span></div>
<div class="cnblogs_code"><img id="code_img_closed_dd068d45-d0fe-4147-9ad3-fe9cd3ec2eb4" class="code_img_closed" src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" alt=""><img id="code_img_opened_dd068d45-d0fe-4147-9ad3-fe9cd3ec2eb4" class="code_img_opened" style="display: none" src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" alt="">
<div id="cnblogs_code_open_dd068d45-d0fe-4147-9ad3-fe9cd3ec2eb4" class="cnblogs_code_hide">
<pre><span style="color: #008080"> 1</span> <span style="color: #008000">#</span><span style="color: #008000">Author:ZhangKanghui</span>
<span style="color: #008080"> 2</span>
<span style="color: #008080"> 3</span> <span style="color: #0000ff">import</span><span style="color: #000000"> threading,time
</span><span style="color: #008080"> 4</span> <span style="color: #0000ff">import</span><span style="color: #000000"> queue
</span><span style="color: #008080"> 5</span>
<span style="color: #008080"> 6</span> q =<span style="color: #000000"> queue.Queue()
</span><span style="color: #008080"> 7</span> <span style="color: #0000ff">def</span><span style="color: #000000"> Producer(namne):
</span><span style="color: #008080"> 8</span> count =<span style="color: #000000"> 0
</span><span style="color: #008080"> 9</span> <span style="color: #0000ff">for</span> i <span style="color: #0000ff">in</span> range(10<span style="color: #000000">):
</span><span style="color: #008080">10</span> q.put(<span style="color: #800000">"</span><span style="color: #800000">骨头%s</span><span style="color: #800000">"</span>%<span style="color: #000000">count)
</span><span style="color: #008080">11</span> <span style="color: #0000ff">print</span>(<span style="color: #800000">"</span><span style="color: #800000">生产了骨头</span><span style="color: #800000">"</span><span style="color: #000000">,count)
</span><span style="color: #008080">12</span> count +=1
<span style="color: #008080">13</span> <span style="color: #008000">#</span><span style="color: #008000">time.sleep(2)</span>
<span style="color: #008080">14</span> time.sleep(1<span style="color: #000000">)
</span><span style="color: #008080">15</span> <span style="color: #008000">#</span><span style="color: #008000">time.sleep(0.5)</span>
<span style="color: #008080">16</span>
<span style="color: #008080">17</span> <span style="color: #0000ff">def</span><span style="color: #000000"> Consumer(name):
</span><span style="color: #008080">18</span> <span style="color: #0000ff">while</span><span style="color: #000000"> True:
</span><span style="color: #008080">19</span> <span style="color: #0000ff">print</span>(<span style="color: #800000">"</span><span style="color: #800000">[%s] 取到 [%s] 并吃了它...</span><span style="color: #800000">"</span>%<span style="color: #000000">(name,q.get()))
</span><span style="color: #008080">20</span>
<span style="color: #008080">21</span> p =threading.Thread(target=Producer,args=(<span style="color: #800000">"</span><span style="color: #800000">Kanghui</span><span style="color: #800000">"</span><span style="color: #000000">,))
</span><span style="color: #008080">22</span> c =threading.Thread(target=Consumer,args=(<span style="color: #800000">"</span><span style="color: #800000">erha</span><span style="color: #800000">"</span><span style="color: #000000">,))
</span><span style="color: #008080">23</span> c1 =threading.Thread(target=Consumer,args=(<span style="color: #800000">"</span><span style="color: #800000">taidi</span><span style="color: #800000">"</span><span style="color: #000000">,))
</span><span style="color: #008080">24</span>
<span style="color: #008080">25</span> <span style="color: #000000">p.start()
</span><span style="color: #008080">26</span> <span style="color: #000000">c.start()
</span><span style="color: #008080">27</span> c1.start()</pre>
</div>
<span class="cnblogs_code_collapse">生产者消费者模型2</span></div>
<p>&nbsp;</p>
<blockquote>
<p><span style="font-size: 14pt"><strong>4 小结</strong></span></p>
</blockquote>
<p>志不坚者智不达。</p>
<p>撑不住的时候可以对自己说声“我好累”,但永远不要再心里承认“我不行”。</p>
<p>没有鸡汤就没有生活。</p>
<p>&nbsp;</p>
<blockquote>
<p><span style="font-size: 14pt"><strong>5 练习</strong></span></p>
</blockquote>
<p>需求:</p>
<ol>
<li>主机分组</li>
<li>登录后显示主机分组,选择分组后查看主机列表</li>
<li>可批量执行命令、发送文件,结果实时返回</li>
<li>主机用户名密码可以不同</li>
</ol>
<p>&nbsp;</p>
<p><strong>流程图:</strong></p>
<p><strong><img src="https://images2018.cnblogs.com/blog/1435523/201809/1435523-20180903145959431-1674715659.png" alt=""></strong></p>
<p>&nbsp;</p>
<div class="cnblogs_code"><img id="code_img_closed_cd6358f5-8a69-4ab5-9c67-cdf1ca5f7216" class="code_img_closed" src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" alt=""><img id="code_img_opened_cd6358f5-8a69-4ab5-9c67-cdf1ca5f7216" class="code_img_opened" style="display: none" src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" alt="">
<div id="cnblogs_code_open_cd6358f5-8a69-4ab5-9c67-cdf1ca5f7216" class="cnblogs_code_hide">
<pre><span style="color: #008080"> 1</span> <span style="color: #008000">#</span><span style="color: #008000">## 作者介绍:</span>
<span style="color: #008080"> 2</span> *<span style="color: #000000"> author:lzl
</span><span style="color: #008080"> 3</span> <span style="color: #008000">#</span><span style="color: #008000">## 博客地址:</span>
<span style="color: #008080"> 4</span> * http://www.cnblogs.com/lianzhilei/p/5881434<span style="color: #000000">.html
</span><span style="color: #008080"> 5</span>
<span style="color: #008080"> 6</span> <span style="color: #008000">#</span><span style="color: #008000">## 功能实现</span>
<span style="color: #008080"> 7</span> <span style="color: #000000">题目:简单主机批量管理工具
</span><span style="color: #008080"> 8</span>
<span style="color: #008080"> 9</span> <span style="color: #000000"> 需求:
</span><span style="color: #008080">10</span> <span style="color: #000000"> 主机分组
</span><span style="color: #008080">11</span> <span style="color: #000000"> 登录后显示主机分组,选择分组后查看主机列表
</span><span style="color: #008080">12</span> <span style="color: #000000"> 可批量执行命令、发送文件,结果实时返回
</span><span style="color: #008080">13</span> <span style="color: #000000"> 主机用户名密码可以不同
</span><span style="color: #008080">14</span>
<span style="color: #008080">15</span> <span style="color: #008000">#</span><span style="color: #008000">## 目录结构:</span>
<span style="color: #008080">16</span> Host-<span style="color: #000000">Manage
</span><span style="color: #008080">17</span> <span style="color: #000000"> │
</span><span style="color: #008080">18</span> ├── ftpclient <span style="color: #008000">#</span><span style="color: #008000">客户端程序</span>
<span style="color: #008080">19</span> <span style="color: #000000"> ├── README.txt
</span><span style="color: #008080">20</span> ├── management.py <span style="color: #008000">#</span><span style="color: #008000">服务端入口程序</span>
<span style="color: #008080">21</span> ├── database <span style="color: #008000">#</span><span style="color: #008000">数据库</span>
<span style="color: #008080">22</span> ├── test.py <span style="color: #008000">#</span><span style="color: #008000">修改数据库</span>
<span style="color: #008080">23</span>
<span style="color: #008080">24</span>
<span style="color: #008080">25</span> <span style="color: #008000">#</span><span style="color: #008000">## 注释</span>
<span style="color: #008080">26</span> <span style="color: #000000"> 可批量执行命令、发送文件
</span><span style="color: #008080">27</span> 上传命令格式: put database /tmp/<span style="color: #000000">db
</span><span style="color: #008080">28</span>
<span style="color: #008080">29</span>
<span style="color: #008080">30</span> <span style="color: #008000">#</span><span style="color: #008000">## 运行环境</span>
<span style="color: #008080">31</span> <span style="color: #000000"> windows系统
</span><span style="color: #008080">32</span> python3.0+
<span style="color: #008080">33</span>
<span style="color: #008080">34</span> README</pre>
</div>
<span class="cnblogs_code_collapse">Readme</span></div>
<div class="cnblogs_code"><img id="code_img_closed_95a7ee23-0f9a-46de-abb4-283ec9bcbda9" class="code_img_closed" src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" alt=""><img id="code_img_opened_95a7ee23-0f9a-46de-abb4-283ec9bcbda9" class="code_img_opened" style="display: none" src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" alt="">
<div id="cnblogs_code_open_95a7ee23-0f9a-46de-abb4-283ec9bcbda9" class="cnblogs_code_hide">
<pre><span style="color: #008080"> 1</span> <span style="color: #0000ff">import</span><span style="color: #000000"> json
</span><span style="color: #008080"> 2</span> <span style="color: #0000ff">import</span><span style="color: #000000"> paramiko
</span><span style="color: #008080"> 3</span> <span style="color: #0000ff">import</span><span style="color: #000000"> threading
</span><span style="color: #008080"> 4</span>
<span style="color: #008080"> 5</span> <span style="color: #0000ff">class</span><span style="color: #000000"> Remotehost(object):
</span><span style="color: #008080"> 6</span> <span style="color: #008000">#</span><span style="color: #008000">远程操作主机</span>
<span style="color: #008080"> 7</span> <span style="color: #0000ff">def</span> <span style="color: #800080">__init__</span><span style="color: #000000">(self,host,port,username,password,cmd):
</span><span style="color: #008080"> 8</span> self.host =<span style="color: #000000"> host
</span><span style="color: #008080"> 9</span> self.port =<span style="color: #000000"> port
</span><span style="color: #008080">10</span> self.username =<span style="color: #000000"> username
</span><span style="color: #008080">11</span> self.password =<span style="color: #000000"> password
</span><span style="color: #008080">12</span> self.cmd =<span style="color: #000000"> cmd
</span><span style="color: #008080">13</span>
<span style="color: #008080">14</span> <span style="color: #0000ff">def</span><span style="color: #000000"> command(self):
</span><span style="color: #008080">15</span> <span style="color: #008000">#</span><span style="color: #008000">获取命令</span>
<span style="color: #008080">16</span> ssh =<span style="color: #000000"> paramiko.SSHClient()
</span><span style="color: #008080">17</span> ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) <span style="color: #008000">#</span><span style="color: #008000"> 允许连接不在know_hosts文件中的主机</span>
<span style="color: #008080">18</span> ssh.connect(hostname=self.host, port=self.port, username=self.username, password=self.password) <span style="color: #008000">#</span><span style="color: #008000"> 连接服务器</span>
<span style="color: #008080">19</span> stdin, stdout, stderr = ssh.exec_command(self.cmd) <span style="color: #008000">#</span><span style="color: #008000"> 获取命令结果</span>
<span style="color: #008080">20</span> res ,err = stdout.read(),stderr.read() <span style="color: #008000">#</span><span style="color: #008000"> 三元运算</span>
<span style="color: #008080">21</span> result = res <span style="color: #0000ff">if</span> res <span style="color: #0000ff">else</span><span style="color: #000000"> err
</span><span style="color: #008080">22</span> <span style="color: #0000ff">print</span>(<span style="color: #800000">"</span><span style="color: #800000">[%s]</span><span style="color: #800000">"</span>.center(50,<span style="color: #800000">"</span><span style="color: #800000">-</span><span style="color: #800000">"</span>)%<span style="color: #000000">self.host)
</span><span style="color: #008080">23</span> <span style="color: #0000ff">print</span>(result.decode()) <span style="color: #008000">#</span><span style="color: #008000"> 打印输出</span>
<span style="color: #008080">24</span> <span style="color: #000000"> ssh.close()
</span><span style="color: #008080">25</span>
<span style="color: #008080">26</span> <span style="color: #0000ff">def</span><span style="color: #000000"> put(self):
</span><span style="color: #008080">27</span> <span style="color: #008000">#</span><span style="color: #008000">上传</span>
<span style="color: #008080">28</span> <span style="color: #0000ff">try</span><span style="color: #000000">:
</span><span style="color: #008080">29</span> transport =<span style="color: #000000"> paramiko.Transport((self.host, self.port))
</span><span style="color: #008080">30</span> transport.connect(username=self.username, password=<span style="color: #000000">self.password)
</span><span style="color: #008080">31</span> sftp =<span style="color: #000000"> paramiko.SFTPClient.from_transport(transport)
</span><span style="color: #008080">32</span> sftp.put(self.cmd.split()[1], self.cmd.split()[2]) <span style="color: #008000">#</span><span style="color: #008000"> 上传文件</span>
<span style="color: #008080">33</span> <span style="color: #000000"> transport.close()
</span><span style="color: #008080">34</span> <span style="color: #0000ff">print</span>(<span style="color: #800000">"</span><span style="color: #800000">\033[32;0m【%s】 上传 文件【%s】 成功....\033[0m</span><span style="color: #800000">"</span>%(self.host,self.cmd.split()[2<span style="color: #000000">]))
</span><span style="color: #008080">35</span> <span style="color: #0000ff">except</span> Exception as error: <span style="color: #008000">#</span><span style="color: #008000"> 抓住异常</span>
<span style="color: #008080">36</span> <span style="color: #0000ff">print</span>(<span style="color: #800000">"</span><span style="color: #800000">\033[31;0m错误:【%s】【%s】\033[0m</span><span style="color: #800000">"</span>%<span style="color: #000000">(self.host,error))
</span><span style="color: #008080">37</span>
<span style="color: #008080">38</span> <span style="color: #0000ff">def</span><span style="color: #000000"> run(self):
</span><span style="color: #008080">39</span> <span style="color: #008000">#</span><span style="color: #008000">反射</span>
<span style="color: #008080">40</span> cmd_str =<span style="color: #000000"> self.cmd.split()[0]
</span><span style="color: #008080">41</span> <span style="color: #0000ff">if</span><span style="color: #000000"> hasattr(self,cmd_str):
</span><span style="color: #008080">42</span> <span style="color: #000000"> getattr(self,cmd_str)()
</span><span style="color: #008080">43</span> <span style="color: #0000ff">else</span><span style="color: #000000">:
</span><span style="color: #008080">44</span> <span style="color: #000000"> setattr(self,cmd_str,self.command)
</span><span style="color: #008080">45</span> <span style="color: #000000"> getattr(self,cmd_str)()
</span><span style="color: #008080">46</span>
<span style="color: #008080">47</span>
<span style="color: #008080">48</span> <span style="color: #0000ff">if</span> <span style="color: #800080">__name__</span> == <span style="color: #800000">"</span><span style="color: #800000">__main__</span><span style="color: #800000">"</span><span style="color: #000000">:
</span><span style="color: #008080">49</span> <span style="color: #008000">#</span><span style="color: #008000">主程序</span>
<span style="color: #008080">50</span> with open(<span style="color: #800000">"</span><span style="color: #800000">database</span><span style="color: #800000">"</span>,<span style="color: #800000">"</span><span style="color: #800000">r</span><span style="color: #800000">"</span><span style="color: #000000">) as file:
</span><span style="color: #008080">51</span> data_dict = json.loads(file.read()) <span style="color: #008000">#</span><span style="color: #008000">获取数据库信息</span>
<span style="color: #008080">52</span> <span style="color: #0000ff">for</span> k <span style="color: #0000ff">in</span> data_dict: <span style="color: #008000">#</span><span style="color: #008000">打印地址组</span>
<span style="color: #008080">53</span> <span style="color: #0000ff">print</span><span style="color: #000000">(k)
</span><span style="color: #008080">54</span>
<span style="color: #008080">55</span> group_choice = input(<span style="color: #800000">"</span><span style="color: #800000">输入要操作的组名:</span><span style="color: #800000">"</span><span style="color: #000000">).strip()
</span><span style="color: #008080">56</span> <span style="color: #0000ff">if</span><span style="color: #000000"> data_dict.get(group_choice):
</span><span style="color: #008080">57</span> host_dict = data_dict[group_choice] <span style="color: #008000">#</span><span style="color: #008000">定义主机字典</span>
<span style="color: #008080">58</span> <span style="color: #0000ff">for</span> k <span style="color: #0000ff">in</span> host_dict: <span style="color: #008000">#</span><span style="color: #008000">打印所选地址组所有的主机名</span>
<span style="color: #008080">59</span> <span style="color: #0000ff">print</span><span style="color: #000000">(k)
</span><span style="color: #008080">60</span> <span style="color: #0000ff">while</span><span style="color: #000000"> True:
</span><span style="color: #008080">61</span> cmd = input(<span style="color: #800000">"</span><span style="color: #800000">选择进行的操作的命令:</span><span style="color: #800000">"</span><span style="color: #000000">).strip()
</span><span style="color: #008080">62</span> thread_list=<span style="color: #000000">[]
</span><span style="color: #008080">63</span> <span style="color: #0000ff">if</span> cmd: <span style="color: #008000">#</span><span style="color: #008000">命令不为空</span>
<span style="color: #008080">64</span> <span style="color: #0000ff">for</span> k <span style="color: #0000ff">in</span><span style="color: #000000"> host_dict:
</span><span style="color: #008080">65</span> host, port, username, password=k,host_dict[k][<span style="color: #800000">"</span><span style="color: #800000">port</span><span style="color: #800000">"</span>],host_dict[k][<span style="color: #800000">"</span><span style="color: #800000">username</span><span style="color: #800000">"</span>],host_dict[k][<span style="color: #800000">"</span><span style="color: #800000">password</span><span style="color: #800000">"</span><span style="color: #000000">]
</span><span style="color: #008080">66</span> func = Remotehost(host,port,username,password,cmd) <span style="color: #008000">#</span><span style="color: #008000">实例化类</span>
<span style="color: #008080">67</span> t = threading.Thread(target=func.run) <span style="color: #008000">#</span><span style="color: #008000">创建线程</span>
<span style="color: #008080">68</span> <span style="color: #000000"> t.start()
</span><span style="color: #008080">69</span> <span style="color: #000000"> thread_list.append(t)
</span><span style="color: #008080">70</span> <span style="color: #0000ff">for</span> t <span style="color: #0000ff">in</span><span style="color: #000000"> thread_list:
</span><span style="color: #008080">71</span> t.join() <span style="color: #008000">#</span><span style="color: #008000">等待线程执行结果</span>
<span style="color: #008080">72</span> <span style="color: #0000ff">else</span><span style="color: #000000">:
</span><span style="color: #008080">73</span> <span style="color: #0000ff">print</span>(<span style="color: #800000">"</span><span style="color: #800000">\033[31;0m操作组不存在\033[0m</span><span style="color: #800000">"</span>)</pre>
</div>
<span class="cnblogs_code_collapse">简单主机批量管理工具主程序</span></div>
<p>相关参考:<a href="https://www.cnblogs.com/0zcl/p/6352278.html" target="_blank">https://www.cnblogs.com/0zcl/p/6352278.html</a></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><strong>我是尾巴~</strong></p>
<p>这次推荐:摄影app合集  <a href="https://mp.weixin.qq.com/s/3N3m7otgKIZXGyUpGKbTpw" target="_blank">https://mp.weixin.qq.com/s/3N3m7otgKIZXGyUpGKbTpw</a></p>
<p>虽不才,才要坚持。</p></div><div id="MySignature"></div>
<div class="clear"></div>
<div id="blog_post_info_block">
<div id="BlogPostCategory"></div>
<div id="EntryTag"></div>
<div id="blog_post_info">
</div>
<div class="clear"></div>
<div id="post_next_prev"></div>
</div> </div>
<div class = "postDesc">posted @ <span id="post-date">2018-09-03 15:09</span> <a href='https://www.cnblogs.com/zhangkanghui/'>m1racle</a> 阅读(<span id="post_view_count">...</span>) 评论(<span id="post_comment_count">...</span>) <a href ="https://i.cnblogs.com/EditPosts.aspx?postid=9577714" rel="nofollow">编辑</a> <a href="#" onclick="AddToWz(9577714);return false;">收藏</a></div>
</div>
<script type="text/javascript">var allowComments=true,cb_blogId=442473,cb_entryId=9577714,cb_blogApp=currentBlogApp,cb_blogUserGuid='a821bb9d-4d70-4e51-5253-08d5ddce222f',cb_entryCreatedDate='2018/9/3 15:09:00';loadViewCount(cb_entryId);var cb_postType=1;</script> </div><!--end: topics 文章、评论容器-->
</div><a name="!comments"></a><div id="blog-comments-placeholder"></div><script type="text/javascript">var commentManager = new blogCommentManager();commentManager.renderComments(0);</script>
<div id='comment_form' class='commentform'>
<a name='commentform'></a>
<div id='divCommentShow'></div>
<div id='comment_nav'><span id='span_refresh_tips'></span><a href='javascript:void(0);' onclick='return RefreshCommentList();' id='lnk_RefreshComments' runat='server' clientidmode='Static'>刷新评论</a><a href='#' onclick='return RefreshPage();'>刷新页面</a><a href='#top'>返回顶部</a></div>
<div id='comment_form_container'></div>
<div class='ad_text_commentbox' id='ad_text_under_commentbox'></div>
<div id='ad_t2'></div>
<div id='opt_under_post'></div>
<div id='cnblogs_c1' class='c_ad_block'></div>
<div id='under_post_news'></div>
<div id='cnblogs_c2' class='c_ad_block'></div>
<div id='under_post_kb'></div>
<div id='HistoryToday' class='c_ad_block'></div>
<script type='text/javascript'>
fixPostBody();
setTimeout(function () { incrementViewCount(cb_entryId); }, 50);
deliverAdT2();
deliverAdC1();
deliverAdC2();
loadNewsAndKb();
loadBlogSignature();
LoadPostInfoBlock(cb_blogId, cb_entryId, cb_blogApp, cb_blogUserGuid);
GetPrevNextPost(cb_entryId, cb_blogId, cb_entryCreatedDate, cb_postType);
loadOptUnderPost();
GetHistoryToday(cb_blogId, cb_blogApp, cb_entryCreatedDate);
</script>
</div> </div><!--end: forFlow -->
</div><!--end: mainContent 主体内容容器--> <div id="sideBar">
<div id="sideBarMain"> <!--done-->
<div class="newsItem">
<h3 class="catListTitle">公告</h3>
<div id="blog-news"></div><script type="text/javascript">loadBlogNews();</script>
</div> <div id="blog-calendar" style="display:none"></div><script type="text/javascript">loadBlogDefaultCalendar();</script> <div id="leftcontentcontainer">
<div id="blog-sidecolumn"></div><script type="text/javascript">loadBlogSideColumn();</script>
</div> </div><!--end: sideBarMain -->
</div><!--end: sideBar 侧边栏容器 -->
<div class="clear"></div>
</div><!--end: main -->
<div class="clear"></div>
<div id="footer"> <!--done-->
Copyright &copy;2018 m1racle
</div><!--end: footer -->
</div><!--end: home 自定义的最大容器 -->
</body>
</html>

来看看爬到了什么

如果同时爬多个网站呢?

 #Author:ZhangKanghui

 from urllib import request
import gevent
import time
from gevent import monkey
#它的作用就是把当前程序的所有io操作单独做上标记
monkey.patch_all() #试一下有它没它的区别 def f(url):
print("GET:%s"%url)
resp = request.urlopen(url)
data = resp.read()
print("%d bytes received from %s."%(len(data),url)) urls = ["https://www.yahoo.com/",
"https://www.python.org/",
"https://www.github.com/",
]
time_start = time.time()
for url in urls:
f(url)
print("同步cost:",time.time() - time_start) aysnc_time_start = time.time()
gevent.joinall(
[gevent.spawn(f,"https://www.yahoo.com/"),
gevent.spawn(f,"https://www.python.org/"),
gevent.spawn(f,"https://www.github.com/"),
])
print("异步cost:",time.time() - aysnc_time_start)

爬虫gevent

如果想对爬虫进入深入的了解和学习,可以参考这里:

https://www.cnblogs.com/voidking/p/python-crawler-base.html

3 select

select最早于1983年出现在4.2BSD中,它通过一个select()系统调用来监视多个文件描述符的数组,当select()返回后,该数组中就绪的文件描述符便会被内核修改标志位,使得进程可以获得这些文件描述符从而进行后续的读写操作。

select目前几乎在所有的平台上支持,其良好跨平台支持也是它的一个优点,事实上从现在看来,这也是它所剩不多的优点之一。

select的一个缺点在于单个进程能够监视的文件描述符的数量存在最大限制,在Linux上一般为1024,不过可以通过修改宏定义甚至重新编译内核的方式提升这一限制。

另外,select()所维护的存储大量文件描述符的数据结构,随着文件描述符数量的增大,其复制的开销也线性增长。同时,由于网络响应时间的延迟使得大量TCP连接处于非活跃状态,但调用select()会对所有socket进行一次线性扫描,所以这也浪费了一定的开销。

如果想深入了解和学习select、poll、epoll三者的区别,看这里:

http://www.cnblogs.com/alex3714/p/4372426.html

通过select实现socket多并发

 #Author:ZhangKanghui

 import select
import socket
import sys
import queue server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server.bind(('local host',9000))
server.listen(1000) server.setblocking(False) msg_dict = []
inputs = [server,]
outputs = []
while True:
readable ,writerable ,exceptional = select.select(inputs,outputs,inputs)
print(readable,writerable,exceptional)
for i in readable:
if r is server:
conn,addr = server.accept()
print("new connection is coming")
print(conn,addr)
inputs.append(conn) #是因为这个新建立的链接还没发数据过来,现在接受的话程序就报错
#所以要实现这个客户端发数据来时server端能知道,就需要让select再监测
msg_dict [conn] =queue.Queue() #初始化一个队列,后面存要给客户端返回的数据
else:
data = r.recv(1024)
# print("recv:",data)
# print("send done....")
#如果不想立马把数据传给客户端可以这么干
msg_dict[r].put(data)
outputs.append(r) #放入返回的连接队列里 for w in writerable: #要返回客户端的连接列表
data_to_client = msg_dict[w].get()
w.send(data_to_client)
outputs.reverse(w) #确保下次循环的时候writerable不返回已经处理完的连接 for e in exceptional:
if e in outputs:
outputs.remove(e) inputs.remove(e) del msg_dict[e]

通过select实现socket多并发

 import socket
import sys messages = [ b'This is the message. ',
b'It will be sent ',
b'in parts.',
]
server_address = ('localhost', 10000) # Create a TCP/IP socket
socks = [ socket.socket(socket.AF_INET, socket.SOCK_STREAM),
socket.socket(socket.AF_INET, socket.SOCK_STREAM),
] # Connect the socket to the port where the server is listening
print('connecting to %s port %s' % server_address)
for s in socks:
s.connect(server_address) for message in messages: # Send messages on both sockets
for s in socks:
print('%s: sending "%s"' % (s.getsockname(), message) )
s.send(message) # Read responses on both sockets
for s in socks:
data = s.recv(1024)
print( '%s: received "%s"' % (s.getsockname(), data) )
if not data:
print(sys.stderr, 'closing socket', s.getsockname() )

select_socket_client

selectors模块

This module allows high-level and efficient I/O multiplexing, built upon the select module primitives.

Users are encouraged to use this module instead, unless they want precise control over the OS-level primitives used.

 #Author:ZhangKanghui

 import selectors
import socket sel = selectors.DefaultSelector() def accept(sock,mask):
conn,addr = sock.accept()
print("accept",conn,"from",addr,mask)
conn.setblocking(False)
sel.register(conn,selectors.EVENT_READ,read) def read(conn,mask):
data = conn.recv(1024)
if data:
print("echoing",repr(data),'to',conn)
conn.send(data)
else:
print("closing",conn)
sel.unregister(conn)
conn.close() sock = socket.socket()
sock.bind('localhosr',9999)
sock.listen(100)
sock.setblocking(False)
sel.register(sock,selectors.EVENT_READ,accept) while True:
events = sel.select()
for key,mask in events:
callback = key.data
callback(key.fileobj,mask)

selector

4 IO多路复用

同步IO和异步IO,阻塞IO和非阻塞IO分别是什么,到底有什么区别?

是真的不懂,看这个吧:

http://www.cnblogs.com/alex3714/articles/5876749.html

5 小结

这些计算机方面的东西太深入了,有点懵逼,实现不知道怎么写下去了

这一节就这样吧!以后再慢慢补充

别的不确定,但是爬虫的部分以后应该会有后续。

6 练习

这次没有作业

给出一个支持向量机的算法:

D10——C语言基础学PYTHON

 #Author:ZhangKanghui

 from sklearn import svm

 x =[[2,0],[1,1],[2,3]]
y =[0,0,1]
clf =svm.SVC(kernel = "linear")
print(clf)
clf.fit(x,y)
#打印支持向量点
print(clf.support_vectors_)
#打印支持向量点的索引值index
print(clf.support_)
#两个类里面分别有1个支持向量
print(clf.n_support_)
print(clf.predict([[2,0],[2,-1]])) '''
print(clf.predict([2,0]))
不知道问什么这样会有一个一个警告
DeprecationWarning: Passing 1d arrays as data is deprecated in 0.17 and willraise ValueError in 0.19.
Reshape your data either using X.reshape(-1, 1) if your data has a single feature or X.reshape(1, -1) if it contains a single sample.
DeprecationWarning)
'''

SVM

还有进阶版的!

 #Author:ZhangKanghui
print(__doc__) #numpy是python中支持矩阵运算的一个模块
#pylab画图
import numpy as np
import pylab as pl
from sklearn import svm np.random.seed(0)
X =np.r_[np.random.randn(20,2)-[2,2],np.random.rand(20,2)+[2,2]]
# print(X)
Y =[0]*20+[1]*20
# print(Y) clf =svm.SVC(kernel="linear")
clf.fit(X,Y) w =clf.coef_[0]
a = -w[0] / w[1]
xx = np.linspace(-5,5)
yy = a*xx - (clf.intercept_[0]) / w[1] b = clf.support_vectors_[0]
yy_down = a*xx + (b[1] - a *b[0])
b = clf.support_vectors_[-1]
yy_up = a*xx +(b[1] - a *b[0]) pl.plot(xx,yy,"")
pl.plot(xx,yy_down,"")
pl.plot(xx,yy_up,"")\ pl.scatter(clf.support_vectors_[:,0],clf.support_vectors_[:,1],s=80)
pl.scatter(X[:,0],X[:,1],c=Y,cmap=pl.cm.paired) pl.axis("tight")
print(pl.show())

SVM进阶版

我是尾巴~

这次推荐:Scien公布的125个科学前沿问题:https://mp.weixin.qq.com/s/0oK1VWvuiS2znYqOQJQdMg

虽不才,才要坚持!