def scheduler(gendct, start): global cargo coroutine = start while 1: (coroutine, cargo) = gendct[coroutine].next()
from __future__ import generators import sys, time threads = [] TOTALSWITCHES = 10**6 NUMTHREADS = 10**5def null_factory(): def empty(): while1: yield None return empty() def quitter(): for n in xrange(TOTALSWITCHES/NUMTHREADS): yield None def scheduler(): global threads try : while1: for thread in threads: thread.next() except StopIteration: passif __name__ == "__main__" : for i in range(NUMTHREADS): threads.append(null_factory()) threads.append(quitter()) starttime = time.clock() scheduler() print"TOTAL TIME: " , time.clock()-starttime print"TOTAL SWITCHES:" , TOTALSWITCHES print"TOTAL THREADS: " , NUMTHREADS
from
__future__
import
generators
import
time
TIMES = 100000
def
stringops():
for
n
in
xrange(TIMES):
s =
"Mary had a little lamb"
s = s.upper()
s =
"Mary had a little lamb"
s = s.lower()
s =
"Mary had a little lamb"
s = s.replace('a','A')
def
scheduler():
for
n
in
xrange(TIMES):
for
thread
in
threads: thread.next()
def
upper():
while1:
s =
"Mary had a little lamb"
s = s.upper()
yield
None
def
lower():
while1:
s =
"Mary had a little lamb"
s = s.lower()
yield
None
def
replace():
while1:
s =
"Mary had a little lamb"
s = s.replace(
'a'
,
'A'
)
yield
None
if
__name__==
'__main__':
start = time.clock()
stringops()
looptime = time.clock()-start
print"LOOP TIME:"
, looptime
global
threads
threads.append(upper())
threads.append(lower())
threads.append(replace())
start = time.clock()
scheduler()
threadtime = time.clock()-start
print"THREAD TIME:"
, threadtime
class
ThreadPool:
"""Enhanced threads list as class
threads = ThreadPool()
threads.append(threadfunc) # not generator object
if threads.query(num) <<has some property>>:
threads.remove(num)
"""def
__init__(self):
self.threadlist = []
self.threaddict = {}
self.avail =
1def
__getitem__(self, n):
return
self.threadlist[n]
def
append(self, threadfunc, docstring=None):
# Argument is the generator func, not the gen object
# Every threadfunc should contain a docstring
docstring = docstring
or
threadfunc.__doc__
self.threaddict[self.avail] = (docstring, threadfunc())
self.avail +=
1
self.threadlist = [p[
1]
for
p
in
self.threaddict.values()]
return
self.avail-
1# return the threadIDdef
remove(self, threadID):
del
self.threaddict[threadID]
self.threadlist = [p[
1]
for
p
in
self.threaddict.values()]
def
query(self, threadID):
"
Information on thread,
if
it exists (otherwise None)
return
self.threaddict.get(threadID,[None])[0]
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有