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

  1. python多线程学习记录

    1.多线程的创建 import threading t = t.theading.Thread(target, args--) t.SetDeamon(True)//设置为守护进程 t.start() ...

  2. python多线程编程

    Python多线程编程中常用方法: 1.join()方法:如果一个线程或者在函数执行的过程中调用另一个线程,并且希望待其完成操作后才能执行,那么在调用线程的时就可以使用被调线程的join方法join( ...

  3. Python 多线程教程:并发与并行

    转载于: https://my.oschina.net/leejun2005/blog/398826 在批评Python的讨论中,常常说起Python多线程是多么的难用.还有人对 global int ...

  4. python多线程

    python多线程有两种用法,一种是在函数中使用,一种是放在类中使用 1.在函数中使用 定义空的线程列表 threads=[] 创建线程 t=threading.Thread(target=函数名,a ...

  5. python 多线程就这么简单(转)

    多线程和多进程是什么自行google补脑 对于python 多线程的理解,我花了很长时间,搜索的大部份文章都不够通俗易懂.所以,这里力图用简单的例子,让你对多线程有个初步的认识. 单线程 在好些年前的 ...

  6. python 多线程就这么简单(续)

    之前讲了多线程的一篇博客,感觉讲的意犹未尽,其实,多线程非常有意思.因为我们在使用电脑的过程中无时无刻都在多进程和多线程.我们可以接着之前的例子继续讲.请先看我的上一篇博客. python 多线程就这 ...

  7. python多线程监控指定目录

    import win32file import tempfile import threading import win32con import os dirs=["C:\\WINDOWS\ ...

  8. python多线程ssh爆破

    python多线程ssh爆破 Python 0x01.About 爆弱口令时候写的一个python小脚本,主要功能是实现使用字典多线程爆破ssh,支持ip表导入,字典数据导入. 主要使用到的是pyth ...

  9. 【python,threading】python多线程

    使用多线程的方式 1.  函数式:使用threading模块threading.Thread(e.g target name parameters) import time,threading def ...

  10. <转>Python 多线程的单cpu与cpu上的多线程的区别

    你对Python 多线程有所了解的话.那么你对python 多线程在单cpu意义上的多线程与多cpu上的多线程有着本质的区别,如果你对Python 多线程的相关知识想有更多的了解,你就可以浏览我们的文 ...

随机推荐

  1. android framework-下载Android系统源代码

    □ apt-get install git-core curl #先下载这两个工具 □ mkdir android-froyo #建立下载目录 □ cd android-froyo #进入下载目录 □ ...

  2. Wamp错误: Forbidden You don't have permission to access / on this server.

    找到php的配置文件httpd.conf(找不到的话看这篇:http://www.cnblogs.com/liulangmao/p/3569807.html) 在原有的位置文件中找到配置节 <D ...

  3. Along with all the above benefits, you cannot overlook the space efficiency and performance gains in using DataFrames and Dataset APIs for two reasons.

    Of all the developers’ delight, a set of APIs that makes them productive, that are easy to use, and ...

  4. 你会用AngularJS,但你会写AngularJS文档么?

    你会用AngularJS,但你会写AngularJS文档么? 涉及知识:gulp javascript 我们经常在写代码的时候要求写好注释,方便日后维护.但其实注释还有一个重要的用途:生成API文档. ...

  5. poi 抽取execl表面数据源代码工具

    开发中 ,导入导出execl避免不了数据类型格式的校验,在使用poi要使用抽取表面数据,poi暂时不支持单元格抽取,查询poi源码抽取工具类如下,如使用jxl就不必使用,jxl取出的单元格数据已是抽取 ...

  6. Cannon

    Description In Chinese Chess, there is one kind of powerful chessmen called Cannon. It can move hori ...

  7. am335x Linux kernel DTS pinmux 定义记录

    记录am335x TI PDK3.0 Linux Kernel 设备的pinmux 的配置 在TI 的Linux kernel 设备树里面,有很多关于pinctrl-single,pins 的配置, ...

  8. PHP——初学,基础

    代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3. ...

  9. udev规则以及编写

    主要内容: udev简介 如何配置和使用udev 如何编写udev规则 字符串替换和匹配 udev主要作用 编写udev规则实例 难点解析 1. udev简介 1.1 什么是udev? udev是Li ...

  10. JavaScript(一):JavaScript简介

    一.什么是JavaScript JavaScript是一种具有面向对象能力的.解释性的程序设计语言.更具体一点,它是基于对象和事件驱动并具有相对安全性的客户端脚本语言.因为他不需要在一个语言环境下运行 ...