易截截图软件、单文件、免安装、纯绿色、仅160KB

Python几种并发实现方案的性能比较

 
#!/Library/Frameworks/Python.framework/Versions/2.5/bin/python
# encoding: utf-8
import sys, time
import thread
 
SLEEP_TIME = 0.0001
 
def run_benchmark(n, m):
    # print(">> Python 2.5.1, stackless 3.1b3 here (N=%d, M=%d)!\n" % (n, m))
    locks = [thread.allocate_lock() for i in xrange(n)]
    firstP = cin = []
    cin_lock_id = 0
    for s in xrange(1, n):
        seqn = s
        cout = []
        cout_lock_id = s
        # print("*> s = %d" % (seqn, ))
        thread.start_new_thread(loop, (seqn, locks, cin, cin_lock_id, cout, cout_lock_id))
        cin = cout
        cin_lock_id = cout_lock_id
    else:
        seqn = s+1
        # print("$> s = %d" % (seqn, ))
        thread.start_new_thread(mloop, (seqn, locks, cin, cin_lock_id))
    for r in xrange(m-1, -1, -1):
        # print("+ sending Msg#  %d" % r)
        lock = locks[0]
        lock.acquire()
        firstP.append(r)
        lock.release()
        time.sleep(SLEEP_TIME)
    try:
        while True:
            time.sleep(SLEEP_TIME)
    except:
        pass
def loop(s, locks, cin, cin_lock_id, cout, cout_lock_id):
    while True:
        lock = locks[cin_lock_id]
        lock.acquire()
        if len(cin) > 0:
            r = cin.pop(0)
            lock.release()
        else:
      &n


相关文档:

[Python 学习]2.5版yield之学习心得

  shhgs 发布了关于《 Py 2.5 what's new 之 yield

之后,原来我不是特别关注 yield 的用法,因为对于2.3中加入的yield相对来说功能简单,它是作为一个 generator
不可缺少的一条语句,只要包含它的函数即是一个 generator 。但在2.3中,generator
不能重入,不能在运行过程中修改,不能引发异常,你要么是顺序 ......

python技术资料

python中国论坛     http://www.okpython.com/bbs/index.php
开源社区   http://sourceforge.net/
python官网  http://www.python.org/
pythonIDE BOAhttp://boa-constructor.sourceforge.net/
pythonIDE 大全http://www.oschina.net/project/tag/120
python组件http://py.dw ......

Python 线程操作

在python中如何创建一个线程对象
如果你要创建一个线程对象,很简单,只要你的类继承threading.Thread,然后在__init__里首先调用threading.Thread的__init__方法即可
import threading
class mythread(threading.Thread):
    def __init__(self, threadname):
      & ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号