代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Python
】
多任务时,如何限制启动线程数实例
作者:
guoyh
/ 发布于
2012/3/6
/
863
<div>coding:utf-8</div> <div>'''</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>2012-3-4</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>绾跨▼浣跨敤涓€渚?<span class="Apple-tab-span" style="white-space:pre"> </span>绋嬪簭娴嬭瘯涓猴細鏈?00涓换鍔★紝閲囩敤绾跨▼鎵ц锛屾瘡娆℃渶澶氬悓鏃惰窇10涓嚎绋嬨€愬垯璺?0浠诲姟銆?<span class="Apple-tab-span" style="white-space:pre"> </span>鍒欙細100 涓换鍔? 姣忎釜鎵ц闇€瑕?绉?<span class="Apple-tab-span" style="white-space:pre"> </span>濡傞噰鐢ㄥ崟绾跨▼, 100 * 1 =100绉?<span class="Apple-tab-span" style="white-space:pre"> </span>濡傞噰鐢?0绾跨▼鍚岃窇 100/10 * 1 =10绉?<span class="Apple-tab-span" style="white-space:pre"> </span>浠庡姣旂湅锛屽彲瑙佷娇鐢ㄧ嚎绋嬬殑濂藉</div> <div>'''</div> import datetime</div> import os,sys</div> import re</div> <div>from threading import Thread</div> import time</div> <div>def getNow():#2010-06-04 11:46:04.992000</div> <div> return datetime.datetime.now()</div> <div>output = ""</div> <div></div> <div>class PING(Thread):</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>count = 0</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>global output</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>def __init__(self,ip=None):</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>Thread.__init__(self)</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>self.ip=ip</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>self.__class__.count = self.__class__.count + 1</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>def run(self):</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>time.sleep(1)</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>self.dataoutput = self.ip +self.ip</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>def __del__(self):</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>global output</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>self.__class__.count = self.__class__.count - 1</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>output = ("%s %s -->%s %s \n" %(output,self.ip,self.dataoutput,getNow()))</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>pass</div> <div></div> <div></div> <div><span class="Apple-tab-span" style="white-space:pre"> </span></div> <div> 娴嬭瘯</div> <div>澶氱嚎绋嬪悓鏃舵墽琛?t=None</div> <div>i = 0</div> <div>nums = 10 #鍚屾椂杩愯绾跨▼鏁?count= 100 #闇€瑕佸鐞嗘暟鎹暟</div> <div>while True:</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>if i >= count:</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>break</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>else:</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>T_thread=[]</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>for j in range(nums):</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>if i >= count:</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>break</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>else:</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>t=PING(i)</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>T_thread.append(t)</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>print("i == %s " % (i))</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>i += 1</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>for j in range(len(T_thread)):</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>print("--> %s " % (j))</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>T_thread[j].start()</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>#sleep鏃堕棿鎺ㄧ畻鏂规硶锛岄渶瑕佸ぇ璋冪敤浠诲姟鍑芥暟鎵ц浣跨敤鏃堕棿锛屼笂闈㈠嚱鏁皊leep涓? 锛屽洜姝よ澶囦负2</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>time.sleep(2)</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>del T_thread</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>del t</div> <div>print(output)</div> <div>'''</div> <div>class test:</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>def __init__(self,ip):</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>self.ip=ip</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>def print1(self):</div> <div><span class="Apple-tab-span" style="white-space:pre"> </span>print(self.ip)</div> <div>t=test(50)</div> <div>t.print1()</div> <div>t=test(60)</div> <div>t.print1()</div> <div>''</div>
试试其它关键字
启动线程数实例
同语言下
.
比较两个图片的相似度
.
过urllib2获取带有中文参数的url内容
.
不下载获取远程图片的宽度和高度及文件大小
.
通过qrcode库生成二维码
.
通过httplib发送GET和POST请求
.
Django下解决小文件下载
.
遍历windows的所有窗口并输出窗口标题
.
根据窗口标题调用窗口
.
python 抓取搜狗指定公众号
.
pandas读取指定列
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
guoyh
贡献的其它代码
(
5
)
.
网页变模糊
.
数通设备端口使用情况监控程序
.
读取plist中的值
.
多任务时,如何限制启动线程数实例
.
数通设备配置文件备份
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3