【Python】多线程2
threading模块
import time
import random
import threading
class Inclass:
def __init__(self):
print 'Inclass 初始化'
def execIn(self,i):
rand = int(random.random() * 10)
print i,'---%s--开始执行,暂停%d秒' % (time.ctime(),rand)
time.sleep(rand) class Outclass:
def __init__(self):
print 'OutClass初始化'
def execOut(self):
InC = Inclass()
length = 1000
nloops = range(length)
threads = []
for i in range(length):
t = threading.Thread(target = InC.execIn, args = (i,))
threads.append(t)
for i in nloops:
threads[i].start()
for i in nloops:
threads[i].join() OC = Outclass()
OC.execOut()
版本2:
import time
import random
import threading
class Inclass:
def __init__(self):
print 'Inclass 初始化'
def execIn(self,i):
rand = int(random.random() * 10)
print i,'---%s--开始执行,暂停%d秒' % (time.ctime(),rand)
time.sleep(rand)
return i class MyThread(threading.Thread):
def __init__(self,func,args,name = ''):
threading.Thread.__init__(self)
self.name = name
self.func = func
self.args = args
def getResult(self):
return self.res
def run(self):
self.res = self.func(*self.args) class Outclass:
def __init__(self):
print 'OutClass初始化'
def execOut(self):
InC = Inclass()
length = 1000
threadlen = 10
k = 0
i = 0
while i < length:
nloops = range(threadlen)
threads = []
for j in range(threadlen):
t = MyThread( InC.execIn, (i,))
i += 1
threads.append(t)
for i in nloops:
threads[i].start()
for i in nloops:
threads[i].join() for i in nloops:
print '-----result:',threads[i].getResult() print k,'---%s--开始执行多线程第%d个小循环' % (time.ctime(),k)
k += 1 OC = Outclass()
OC.execOut()
【Python】多线程2的更多相关文章
- python多线程学习记录
1.多线程的创建 import threading t = t.theading.Thread(target, args--) t.SetDeamon(True)//设置为守护进程 t.start() ...
- python多线程编程
Python多线程编程中常用方法: 1.join()方法:如果一个线程或者在函数执行的过程中调用另一个线程,并且希望待其完成操作后才能执行,那么在调用线程的时就可以使用被调线程的join方法join( ...
- Python 多线程教程:并发与并行
转载于: https://my.oschina.net/leejun2005/blog/398826 在批评Python的讨论中,常常说起Python多线程是多么的难用.还有人对 global int ...
- python多线程
python多线程有两种用法,一种是在函数中使用,一种是放在类中使用 1.在函数中使用 定义空的线程列表 threads=[] 创建线程 t=threading.Thread(target=函数名,a ...
- python 多线程就这么简单(转)
多线程和多进程是什么自行google补脑 对于python 多线程的理解,我花了很长时间,搜索的大部份文章都不够通俗易懂.所以,这里力图用简单的例子,让你对多线程有个初步的认识. 单线程 在好些年前的 ...
- python 多线程就这么简单(续)
之前讲了多线程的一篇博客,感觉讲的意犹未尽,其实,多线程非常有意思.因为我们在使用电脑的过程中无时无刻都在多进程和多线程.我们可以接着之前的例子继续讲.请先看我的上一篇博客. python 多线程就这 ...
- python多线程监控指定目录
import win32file import tempfile import threading import win32con import os dirs=["C:\\WINDOWS\ ...
- python多线程ssh爆破
python多线程ssh爆破 Python 0x01.About 爆弱口令时候写的一个python小脚本,主要功能是实现使用字典多线程爆破ssh,支持ip表导入,字典数据导入. 主要使用到的是pyth ...
- 【python,threading】python多线程
使用多线程的方式 1. 函数式:使用threading模块threading.Thread(e.g target name parameters) import time,threading def ...
- <转>Python 多线程的单cpu与cpu上的多线程的区别
你对Python 多线程有所了解的话.那么你对python 多线程在单cpu意义上的多线程与多cpu上的多线程有着本质的区别,如果你对Python 多线程的相关知识想有更多的了解,你就可以浏览我们的文 ...
随机推荐
- NGUI UIPanel绘制原理学习
NGUI底层绘制都是调用UIDrawCall来完成的,它会动态实例化出材质球,改变UV偏移和缩放(图集显示).并且全部是面片 UIPanel也是面片,但是内部物体遮罩比较特殊,经过查找发现,影响UIP ...
- SourceInsight-Symbol not found
使用SourceInsight查看源代码时,发现点击查看相关类型时,无法关联到其代码,出现 symbol not found, 然而明明在我的头文件有定义的 网上查了一下主要是因为新建工程导入文件后, ...
- http请求头中的Content-Type属性在angular 和 node中的用法
post请求的请求体有以下两种格式: 1. 字符串: 'name=code_bunny&age=12' 这种格式的请求体,需要配置请求头 'Content-Type':'application ...
- angular学习笔记(九)-css类和样式1
本篇主要介绍通过数据绑定来给元素添加特定的类名,从而应用特定的样式 从一个最基本的例子来看: <!DOCTYPE html> <html ng-app> <head> ...
- Ubuntu下安装JDK1.7
Ubuntu操作系统下如何手动安装JDK1.7呢?本文是我经历的全过程. Ubuntu版本:12.04 LTS JDK版本:1.7.0_04 安装目录:/usr/local/development/j ...
- BAT-使用BAT方法清理Delphi临时文件
@echo offdel /S *.~*del /S *.dcudel /S *.dskdel /S *.hppdel /S *.ddpdel /S *.mpsdel /S *.mptdel /S * ...
- 一款基于jquery ui漂亮的可拖动div实例
今天要给大家带来一款基于jquery ui漂亮的可拖动div实例.这款实例用了图片当背景,div层采用了幽灵透明效果.div拖动是基于jquery ui的.界面非常漂亮.一起看下效果图: 在线预览 ...
- 关于Unity树形插件Tree View Control的相关搜集
博客http://blog.csdn.net/qq_15267341/article/details/51997926 的这个 Script Based Runtime Tree-Vie ...
- 【C#】遍历List列表的同时,移除访问到的元素
需求:遍历List列表,当访问的元素符合某一条件时,将该元素移除出列表. 注意点:使用foreach循环遍历无法做到边读边修改,所以要使用for循环. 例子: // 倒序遍历. for (int i ...
- C语言 · 寂寞的数
算法训练 寂寞的数 时间限制:1.0s 内存限制:256.0MB 问题描述 道德经曰:一生二,二生三,三生万物. 对于任意正整数n,我们定义d(n)的值为为n加上组成n的各个数字的和 ...