- 浏览: 1999400 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (651)
- ACE (35)
- BAT (9)
- C/C++ (116)
- fast-cgi (14)
- COM (27)
- python (59)
- CGI (4)
- C# (2)
- VC (84)
- DataBase (29)
- Linux (96)
- P2P (6)
- PHP (15)
- Web (6)
- Memcached (7)
- IME输入法 (11)
- 设计模式 (2)
- 搜索引擎 (1)
- 个人情感 (4)
- 笔试/面试 (3)
- 一亩三分地 (33)
- 历史 (2)
- 地理 (1)
- 人物 (3)
- 经济 (0)
- 不仅仅是笑哦 (43)
- 小故事大道理 (2)
- http://www.bjdsmyysjk120.com/ (0)
- http://www.bjdsmyy120.com/ (0)
- 它山之石可以攻玉 (15)
- 大学生你关注些什么 (28)
- 数据恢复 (1)
最新评论
-
luokaichuang:
这个规范里还是没有让我明白当浏览器上传文件时,STDIN的消息 ...
FastCGI规范 -
effort_fan:
好文章!学习了,谢谢分享!
com技术简介 -
vcell:
有错误os.walk(strPath)返回的已经是全部的文件和 ...
通过python获取目录的大小 -
feifeigd:
feifeigd 写道注意:文章中的CPP示例第二行 #inc ...
ATL入门:利用ATL编写简单的COM组件 -
feifeigd:
注意:文章中的CPP示例第二行 #include " ...
ATL入门:利用ATL编写简单的COM组件
2.使用Jython 或 IronPython,可以得到真正的多线程。
...
th = threading.Thread(target=worker,args=(i,acc) ) ;
或者th.join()
g_mutex = threading.Lock()
....
使用锁
for ... :
#锁定,从下一句代码到释放前互斥访问
g_mutex.acquire()
a_account.deposite(1)
#释放
g_mutex.release()
import threading
def worker(a_tid,a_account):
global g_mutex
print "Str " , a_tid, datetime.datetime.now()
for i in range(1000000):
#g_mutex.acquire()
a_account.deposite(1)
#g_mutex.release()
print "End " , a_tid , datetime.datetime.now()
class Account:
def __init__ (self, a_base ):
self.m_amount=a_base
def deposite(self,a_amount):
self.m_amount+=a_amount
def withdraw(self,a_amount):
self.m_amount-=a_amount
if __name__ == "__main__":
global g_mutex
count = 0
dstart = datetime.datetime.now()
print "Main Thread Start At: " , dstart
#init thread_pool
thread_pool = []
#init mutex
g_mutex = threading.Lock()
# init thread items
acc = Account(100)
for i in range(10):
th = threading.Thread(target=worker,args=(i,acc) ) ;
thread_pool.append(th)
# start threads one by one
for i in range(10):
thread_pool[i].start()
#collect all threads
for i in range(10):
threading.Thread.join(thread_pool[i])
dend = datetime.datetime.now()
print "count=",acc.m_amount
print "Main Thread End at: " ,dend , " time span " , dend-dstart;
Str 0 2009-01-13 00:17:55.312000
Str 1 2009-01-13 00:17:55.453000
Str 2 2009-01-13 00:17:55.484000
Str 3 2009-01-13 00:17:55.531000
Str 4 2009-01-13 00:17:55.562000
Str 5 2009-01-13 00:17:55.609000
Str 6 2009-01-13 00:17:55.640000
Str 7 2009-01-13 00:17:55.687000
Str 8 2009-01-13 00:17:55.718000
Str 9 2009-01-13 00:17:55.781000
End 0 2009-01-13 00:18:06.250000
End 1 2009-01-13 00:18:07.500000
End 4 2009-01-13 00:18:07.531000
End 2 2009-01-13 00:18:07.562000
End 3 2009-01-13 00:18:07.593000
End 9 2009-01-13 00:18:07.609000
End 7 2009-01-13 00:18:07.640000
End 8 2009-01-13 00:18:07.671000
End 5 2009-01-13 00:18:07.687000
End 6 2009-01-13 00:18:07.718000
count= 3434612
Main Thread End at: 2009-01-13 00:18:07.718000 time span 0:00:12.422000
Str 0 2009-01-13 00:26:12.156000
Str 1 2009-01-13 00:26:12.390000
Str 2 2009-01-13 00:26:12.437000
Str 3 2009-01-13 00:26:12.468000
Str 4 2009-01-13 00:26:12.515000
Str 5 2009-01-13 00:26:12.562000
Str 6 2009-01-13 00:26:12.593000
Str 7 2009-01-13 00:26:12.640000
Str 8 2009-01-13 00:26:12.671000
Str 9 2009-01-13 00:26:12.718000
End 0 2009-01-13 00:27:01.781000
End 1 2009-01-13 00:27:05.890000
End 5 2009-01-13 00:27:06.046000
End 7 2009-01-13 00:27:06.078000
End 4 2009-01-13 00:27:06.109000
End 2 2009-01-13 00:27:06.140000
End 6 2009-01-13 00:27:06.156000
End 8 2009-01-13 00:27:06.187000
End 3 2009-01-13 00:27:06.203000
End 9 2009-01-13 00:27:06.234000
count= 10000100
Main Thread End at: 2009-01-13 00:27:06.234000 time span 0:00:54.078000
发表评论
-
提供有偿反编译 python2.5,2.6,2.7 code
2014-04-03 16:32 1276提供有偿 反编译 python2.5, python2.6 ... -
Python:封装允许执行命令有超时的类
2012-08-24 17:35 4486封装允许执行命令有超时的类 #!/usr/bin/env ... -
python 去掉重复行
2011-06-16 12:15 6834#!/usr/bin/env python impor ... -
通过python获取目录的大小
2011-06-07 11:04 8863通过python获取目录的大小 需要用到的mod ... -
python 单元测试示例2
2011-05-17 11:51 1512#!/usr/bin/env python ... -
python 单元测试示例1
2011-05-17 11:51 1704#!/usr/bin/env python im ... -
python 获取文件md5
2011-05-13 14:01 7986#!/usr/bin/env python im ... -
python模拟windows获取设置ini
2011-05-05 12:20 1088#!/usr/bin/env python from ... -
python 解析 json
2011-04-25 15:42 3647#!/usr/bin/env python impor ... -
python时间处理
2011-04-23 19:25 674import time; import os; impor ... -
python简单的socket 服务器和客户端
2011-03-03 17:42 11489服务器端代码 if "__main__&qu ... -
python __file__ 与argv[0]
2011-02-28 11:25 36736python __file__ 与argv[0] 在py ... -
python os.path模块 简明文档
2011-02-28 11:10 2054os.path.abspath(path)取path的绝对目录 ... -
Python 用HTMLParser解析HTML文件
2011-02-16 20:44 34916Python 用HTMLParser解析HTML文件 ... -
python使用simplejson解析示例
2011-02-16 15:28 8161#!/usr/bin/env python imp ... -
python simplejson模块的使用方法
2011-02-16 14:38 10280python安装:easy_install simplejso ... -
用PDB库调试Python程序
2011-01-06 12:54 1895如果使用过微软技术的朋友应该体会过微软的Visual Stud ... -
python更新svn
2010-12-29 11:05 6265def UpdateSvn(strUser, strPwd, ... -
python 函数参数的传递(参数带星号的说明)
2010-12-23 17:59 1393python中函数参数的传递是通过赋值来传递的。函数参数的 ... -
python 之 分割参数getopt
2010-12-17 11:02 51322python 之 分割参数getopt ...
相关推荐
Python多线程编程文档说明 多进程编程 一、 multiprocessing 模块 1. multiprocessing 模块提供了多进程编程的能力 它的API非常类似于 threading 模块,但是也提供了一些threading 模块不具有的能力 相比于线程,它...
演示python的多线程编程,以及多个线程之间的同步
1、多线程的理解 多进程和多线程都可以执行多个任务,线程是进程的一部分。...2、Python多线程创建 在Python中,同样可以实现多线程,有两个标准模块thread和threading,不过我们主要使用 更高级的threading模块。
6. 多线程编程:介绍Python的多线程编程、线程的创建、同步、锁等知识。 7. Web开发:介绍Python的Web开发、Django框架、MVC模式、ORM等知识。 8. 数据库操作:介绍Python的数据库操作、MySQL、MongoDB等数据库的...
Python3爬虫系列的理论验证,比较同步依序下载、多进程并发、多线程并发和asyncio异步编程之间的效率差别
在Python中我们主要是通过thread和 threading这两个模块来实现的,其中Python的threading模块是对thread做了一些包装的,可以更加方便的被使用,所以我们使用 threading模块实现多线程编程。这篇文章我们主要来看看...
主要介绍了Python多线程编程(四):使用Lock互斥锁,本文讲解了互斥锁概念、同步阻塞、代码示例等内容,需要的朋友可以参考下
主要介绍了Python多线程编程(七):使用Condition实现复杂同步,本文讲解通过很著名的“生产者-消费者”模型来来演示在Python中使用Condition实现复杂同步,需要的朋友可以参考下
前言 进程之间通信与线程同步是一个历久弥新的话题,对编程稍有了解应该都...python多线程同步 python中提供两个标准库thread和threading用于对线程的支持,python3中已放弃对前者的支持,后者是一种更高层次封装的线
Python多线程爬虫是指使用Python编程语言中的多线程技术来实现网页爬取任务的并发执行。通常在进行网页爬取时,单线程的爬虫需要逐个请求并解析每个网页,由于网络延迟和IO等待导致相应时间较长。而多线程爬虫可以...
上一节记录了多线程技术以及Python多线程的的简单上手.毫无疑问,多线程是为了充分利用硬件资源尤其是CPU资源来提高任务处理效率的技术。将任务拆分为多个线程同时运行,那么属于同一个任务的多个线程之间必然会有...
Python中提供几个用于多线程编程的模块,包括thread,threading和Queue等 thread模块提供了基本的线程和锁的支持,除产生线程外,也提供基本的同步数据结构锁对象,其中包括: start_new_thread(function, args ...
python 08、多任务编程多线程 3-1_进程间通信、同步互斥、threading线程_Day03_AM.mp4
使用threading.Event可以实现线程间相互通信,之前的Python:使用threading模块实现多线程编程七[使用Condition实现复杂同步]我们已经初步实现了线程间通信的基本功能,但是更为通用的一种做法是使用threading.Event...
第3章 多线程 《Python基础实训教程》第3章-多线程全文共23页,当前为第1页。... 3.2.1 Thread方法 Python实现线程创建是利用threading模块完成,开展多线程编程需要提前导入该模块,如: import threading 利用
主要介绍了Python编程scoketServer实现多线程同步实例代码,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下
利用Python实现多窗口订票系统,利用 threading.Lock() 避免出现一票多卖,无票也卖的情况,并规范化输出情况。 代码: import threading import time tickets, lock = 20, threading.Lock() class TicketWindows...
最近学习python并发,于是对多进程、多线程、异步和协程做了个总结。 一、多线程 多线程就是允许一个进程内存在多个控制权,以便让多个函数同时处于激活状态,从而让多个函数的操作同时运行。即使是单CPU的计算机,...