def _test_simple(): n_levels = (1, 2, 5, 10) c_levels = (1, 5, 10, 50) q_sizes = (None, 1, 5, 10, 50) for n, c, q in itertools.product(n_levels, c_levels, q_sizes): with child_service(MyService, n_process=n, concurrency=c, queue_size=q) as client: for i in xrange(n): a = i * 2 + 7 b = i * 3 + 5 expected = a + b result = client.call('sum', a, b) eq_(result, expected) expected_m = a * b result_m = client.call('multiply', a, b) eq_(result_m, expected_m) with child_service(MyService, concurrency=c, queue_size=q) as client: future = client.call_async('sum', 3, 7) value = future.get() eq_(value, 10) cb_data = dict(value=None, called=False) with child_service(MyService, concurrency=c, queue_size=q) as client: def _callback(value): cb_data['value'] = value cb_data['called'] = True client.call_callback(_callback, 'sum', 1, 2) ok_(cb_data['called']) eq_(cb_data['value'], 3)
def main(): # 'concurrency' will control simultaneous processing gleenlet number on service # fork only 1 process for MyService with child_service(MyService, concurrency=10) as client: # async callback def _callback(value): print '_callback: value=%d' % value client.call_callback(_callback, 'sum', 1, 2) # blocking call print 'block call: value=%d' % client.call('sum', 2, 3) # future(gevent.event.AsyncResult object) future = client.call_async('sum', 10, 20) print 'future: value=%d' % future.get() # will block until service side computation finishes
def _test_delay(): n_levels = (1, 2, 3, 4, 5) c_levels = (5, 10, 50) # concurrency=1 can't pass this test on p=1 q_sizes = (None, 5, 10, 50) for p, c, q in itertools.product(n_levels, c_levels, q_sizes): called = [] with child_service(DelayService, n_process=p, concurrency=c, queue_size=q) as client: def _cb(value): print value called.append(value) client.call_callback(_cb, 'delay', 500) # returns 3rd gevent.sleep() client.call_callback(_cb, 'delay', 100) # returns 1st gevent.sleep() client.call_callback(_cb, 'delay', 300) # returns 2nd eq_(called, [100, 300, 500])
def main(): t1 = time.time() n = 10000 with child_service(MyService, n_process=8, concurrency=1) as client: for i in xrange(n): arg = str(i) def _cb(value, arg=None): global count count += 1 # print '%s => %s count=%d' % (arg, value, count) _cb = functools.partial(_cb, arg=arg) # preseve original arg to show debug msg client.call_callback(_cb, 'superhash', arg) def _waiter(): global count while count < n: gevent.sleep(0.1) waiter = gevent.spawn(_waiter) waiter.join() t2 = time.time() print '%fsec' % (t2-t1)