Created
July 4, 2017 06:19
-
-
Save nooperpudd/4643c58d69a6ee8733dfba03b952e2da to your computer and use it in GitHub Desktop.
python3 asyncio command
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import sys | |
async def execute(command, cwd=None, shell=True): | |
process = await asyncio.create_subprocess_exec(*command, | |
stdout=asyncio.subprocess.PIPE, | |
stderr=asyncio.subprocess.PIPE, | |
cwd=cwd, | |
shell=shell) | |
std_out, std_err = await process.communicate() | |
error = std_err.decode().strip() | |
result = std_out.decode().strip() | |
print(result) | |
print(error) | |
return result | |
if sys.platform == "win32": | |
loop = asyncio.ProactorEventLoop() | |
asyncio.set_event_loop(loop) | |
else: | |
loop = asyncio.get_event_loop() | |
try: | |
loop.run_until_complete( | |
asyncio.gather(execute(["bash", "-c", "echo hello && sleep 2"]), execute(["bash", "-c", "echo ok && sleep 1"]))) | |
except Exception as e: | |
raise e | |
finally: | |
loop.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment