fork(1) download
  1. import asyncio, itertools
  2. import time
  3.  
  4. def do_shit():
  5. for i in itertools.count():
  6. print('In background: {}'.format(i))
  7. time.sleep(1)
  8.  
  9. @asyncio.coroutine
  10. def add(x, y):
  11. print('Adding... {} + {}'.format(x, y))
  12. yield from asyncio.sleep(3)
  13. return x + y
  14.  
  15. @asyncio.coroutine
  16. def main_task(x, y):
  17. result = yield from add(x, y)
  18. print('The result was: {}'.format(result))
  19.  
  20.  
  21. def background_task(target, *, loop=None, executor=None):
  22. if loop is None:
  23. loop = asyncio.get_event_loop()
  24.  
  25. if callable(target):
  26. return loop.run_in_executor(executor, target)
  27.  
  28. raise TypeError("target must be a callable, not {!r}".format(type(target)))
  29.  
  30. ticker = background_task(do_shit)
  31. loop = asyncio.get_event_loop()
  32. loop.run_until_complete(main_task(20, 31))
  33. loop.close()
  34.  
Time limit exceeded #stdin #stdout 5s 25000KB
stdin
Standard input is empty
stdout
In background: 0
Adding... 20 + 31
In background: 1
In background: 2
The result was: 51