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

 
  1. #!/Library/Frameworks/Python.framework/Versions/2.5/bin/python
  2. # encoding: utf-8
  3. import sys, time
  4. import thread
  5.  
  6. SLEEP_TIME = 0. 0001
  7.  
  8. def run_benchmark (n, m ):
  9.     # print(">> Python 2.5.1, stackless 3.1b3 here (N=%d, M=%d)!/n" % (n, m))
  10.     locks = [ thread. allocate_lock () for i in xrange (n )]
  11.     firstP = cin = []
  12.     cin_lock_id = 0
  13.     for s in xrange ( 1, n ):
  14.         seqn = s
  15.         cout = []
  16.         cout_lock_id = s
  17.         # print("*> s = %d" % (seqn, ))
  18.         thread. start_new_thread (loop, (seqn, locks, cin, cin_lock_id, cout, cout_lock_id ))
  19.         cin = cout
  20.         cin_lock_id = cout_lock_id
  21.     else:
  22.         seqn = s +1
  23.         # print("$> s = %d" % (seqn, ))
  24.         thread. start_new_thread (mloop, (seqn, locks, cin, cin_lock_id ))
  25.     for r in xrange (m -1, -1, -1 ):
  26.         # print("+ sending Msg#  %d" % r)
  27.         lock = locks [ 0 ]
  28.         lock. acquire ()
  29.         firstP. append (r )
  30.         lock. release ()
  31.         time. sleep (SLEEP_TIME )
  32.     try:
  33.         while True:
  34.             time. sleep (SLEEP_TIME )
  35.     except:
  36.         pass
  37. def loop (s, locks, cin, cin_lock_id, cout, cout_lock_id ):
  38.     while True:
  39.         lock = locks [cin_lock_id ]
  40.         lock. acquire ()
  41.         if len (cin ) > 0:
  42.             r = cin. pop ( 0 )
  43.             lock. release ()
  44.         else:
  45.             lock. release ()
  46.             time. sleep (SLEEP_TIME )
  47.             continue
  48.         lock = locks [cout_lock_id ]
  49.         lock. acquire ()
  50.         cout. append (r )
  51.         lock. release ()
  52.         if r > 0:
  53.             # print(": Proc: <%s>, Seq#: %s, Msg#: %s .." % (pid(), s, r))
  54.             pass
  55.         else:
  56.             # print("* Proc: <%s>, Seq#: %s, Msg#: terminate!" % (pid(), s))
  57.             break
  58. def mloop (s, locks, cin, cin_lock_id ):
  59.     while True:
  60.         lock = locks [cin_lock_id ]
  61.         lock. acquire ()
  62.         if len (cin ) > 0:
  63.             r = cin. pop ( 0 )
  64.             lock. release ()
  65.         else:
  66.             lock. release ()
  67.             time. sleep (SLEEP_TIME )
  68.             continue
  69.         if r > 0:
  70.             # print("> Proc: <%s>, Seq#: %s, Msg#: %s .." % (pid(), s, r))
  71.             pass
  72.         else:
  73.             # print("@ Proc: <%s>, Seq#: %s, ring terminated." % (pid(), s))
  74.             break
  75.     thread. interrupt_main ()
  76.  
  77. def pid (): return thread. get_ident ()
  78.  
  79. if __name__ == '__main__':
  80.     run_benchmark ( int ( sys. argv [ 1 ]), int ( sys. argv [ 2 ]))

 

 

  1. #!/Library/Frameworks/Python.framework/Versions/2.5/bin/python
  2. # encoding: utf-8
  3. import sys
  4. import threading, Queue
  5.  
  6. def run_benchmark (n, m ):
  7.     # print(">> Python 2.5.1, stackless 3.1b3 here (N=%d, M=%d)!/n" % (n, m))
  8.     firstP = cin = Queue. Queue ()
  9.     for s in xrange ( 1, n ):
  10.         seqn = s
  11.         cout = Queue. Queue ()
  12.         # print("*> s = %d" % (seqn, ))
  13.         t = Loop (seqn, cin, cout )
  14.         t. setDaemon ( False )
  15.         t. start ()
  16.         cin = cout
  17.     else:
  18.         seqn = s +1
  19.         # print("$> s = %d" % (seqn, ))
  20.         t = MLoop (seqn, cin )
  21.         t. setDaemon ( False )
  22.         t. start ()
  23.     for r in xrange (m -1, -1, -1 ):
  24.         # print("+ sending Msg#  %d" % r)
  25.         firstP. put (r )
  26. class Loop ( threading. Thread ):
  27.     def __init__ ( self, s, cin, cout ):
  28.         threading. Thread. __init__ ( self )
  29.         self. cin = cin
  30.         self. cout = cout
  31.         self. s = s
  32.     def run ( self ):
  33.         while True:
  34.             r = self. cin. get ()
  35.             self. cout. put (r )
  36.             if r > 0:
  37.                   # print(": Proc: <%s>, Seq#: %s, Msg#: %s .." % (pid(), self.s, r))
  38.                   pass
  39.             else:
  40.                   # print("* Proc: <%s>, Seq#: %s, Msg#: terminate!" % (pid(), self.s))
  41.                   break
  42. class MLoop ( threading. Thread ):
  43.     def __init__ ( self, s, cin ):
  44.         threading. Thread. __init__ ( self )
  45.         self. cin = cin
  46.         self. s = s
  47.     def run ( self ):
  48.         while True:
  49.             r = self. cin. get ()
  50.             if r > 0:
  51.                 # print("> Proc: <%s>, Seq#: %s, Msg#: %s .." % (pid(), self.s, r))
  52.                 pass
  53.             else:
  54.                 # print("@ Proc: <%s>, Seq#: %s, ring terminated." % (pid(), self.s))
  55.                 break
  56.  
  57. def pid (): return threading. currentThread ()
  58.  
  59. if __name__ == '__main__':
  60.     run_benchmark ( int ( sys. argv [ 1 ]), int ( sys. argv [ 2 ]))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值