CandyGL 2016-03-02
例子:
import gevent
from gevent import monkey
import urllib2
from gevent.pool import Pool
import datetime,sys
def resquest01():
print "resquest start..."
for i in threads:
print i
thread1 = int(threads[0])
print type(thread1)
print "thread1"
print thread1
print "thread1"
for n in loops:
print n
loop1 = int(loops[0])
print type(loop1)
print "loop1"
print loop1
print "loop1"
print('Running in foo')
gevent.sleep(0)
print('Explicit context switch to foo again')
#start_time = time.ctime()
#print start_time
urls = ['http://10.154.156.238:8000'] * loop1
pool = Pool(thread1)
print pool.map(download,urls)
#end_time = time.ctime()
#print end_time
print '-------------------------------------------------------'
def resquest02():
print "resquest start..."
for i in threads:
print i
thread2 = int(threads[1])
print type(thread2)
print "thread2"
print thread2
print "thread2"
for n in loops:
print n
loop2 = int(loops[1])
print type(loop2)
print "loop2"
print loop2
print "loop2"
print('Explicit context to bar')
gevent.sleep(0)
print('Implicit context switch back to bar')
urls = ['http://10.154.156.238:8000/error'] * loop2
pool = Pool(thread2)
print pool.map(download,urls)
def download(urls):
start_time = datetime.datetime.now()
print start_time
try:
respones = urllib2.urlopen(urls)
code = respones.getcode()
print code
except urllib2.HTTPError,e:
print e.code
end_time = datetime.datetime.now()
print end_time
elapsed_time = (start_time - end_time).seconds
print 'elapsed_time:' + str(elapsed_time)
#return urllib2.urlopen(urls).read()
#gevent.joinall([gevent.spawn(foo),gevent.spawn(bar),])
if __name__ == '__main__':
print "start testing..."
origin = sys.argv[1:]
print origin
threads1 = 1
print type(threads1)
threads2 = 2
print type(threads2)
global loops,threads,urls
loops = []
threads = []
urls = []
for index, i in enumerate(origin):
if index % 3 == 2:
print 'index is %d, i is %s' % (index,i)
loops.append(i)
if index % 3 == 1:
print 'index is %d ,i is %s' % (index,i)
threads.append(i)
if index % 3 == 0:
print 'index is %d , i is %s' % (index,i)
urls.append(i)
print loops
print threads
print urls
print "main() end..."
#loop = i
#print loop
gevent.joinall([gevent.spawn(resquest01),gevent.spawn(resquest02),])