Advertisement
Guest User

Untitled

a guest
Sep 7th, 2020
426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.80 KB | None | 0 0
  1. import asyncio
  2.  
  3. async def run(cmd):
  4.     proc = await asyncio.create_subprocess_shell(
  5.         cmd,
  6.         stdout=asyncio.subprocess.PIPE,
  7.         stderr=asyncio.subprocess.PIPE)
  8.  
  9.     stdout, stderr = await proc.communicate()
  10.  
  11.     print(f'[{cmd!r} exited with {proc.returncode}]')
  12.     if stdout:
  13.         print(f'[stdout]\n{stdout.decode()}')
  14.     if stderr:
  15.         print(f'[stderr]\n{stderr.decode()}')
  16.  
  17. def go_do_something(index):
  18.     await run("sleep 2")
  19.        
  20. def my_long_func(val: int) -> None:
  21.         """
  22.        This function contains a loop
  23.        Each iteration of the loop calls a function
  24.        Nothin is returned
  25.        """
  26.         for index in range(val):
  27.             print("index = "+str(index))
  28.             go_do_something(index)
  29.  
  30. my_long_func(3) # launch three tasks
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement