Skip to content

Instantly share code, notes, and snippets.

@tonicmuroq
Created August 14, 2015 09:44
Show Gist options
  • Save tonicmuroq/da4fa1b5051779592714 to your computer and use it in GitHub Desktop.
Save tonicmuroq/da4fa1b5051779592714 to your computer and use it in GitHub Desktop.
server simply get `value` from querystring and returns it, request simply pass `value` in querystring and tests if the return value equals `value`
# coding: utf-8
import random
import requests
import multiprocessing
def request_once():
randint = random.randint(1, 10000)
url = 'http://localhost:8000?value=%s' % randint
r = requests.get(url)
if r.content != str(randint):
print 'expect %s, got %s' % (randint, r.content)
else:
print '-------------------'
def m():
for i in range(10):
request_once()
pool = multiprocessing.Pool(processes=30)
[pool.apply_async(m) for _ in range(30)]
pool.close()
pool.join()
# coding: utf-8
import tornado
import tornado.web
from tornado import ioloop, gen, httpclient
from tornado.web import RequestHandler
class Holder(object):
pass
holder = Holder()
@gen.engine
def outside_function(callback, value):
holder.value = value
yield gen.Task(httpclient.AsyncHTTPClient().fetch, 'https://www.google.com')
callback(value)
class MasterHandler(RequestHandler):
@gen.coroutine
def get(self):
holder.value = self.get_argument('value', 'default')
yield gen.Task(outside_function, value=holder.value)
self.finish(str(holder.value))
urls = [
(r'/', MasterHandler),
]
app = tornado.web.Application(urls)
app.listen(8000)
ioloop.IOLoop().instance().start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment