import asyncio import sys import os async def runexecutable(executablepath, args=[]): if…
- User
import asyncio import sys import os
async def run_executable(executable_path, args=[]): if sys.platform == "win32": cmd = [executable_path] + args else: # Prepends wine to the command if not on Windows. cmd = ["wine", executable_path] + args
# Running the executable without opening a terminal window proc = await asyncio.create_subprocess_exec( *cmd, # Capture output and error stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE) stdout, stderr = await proc.communicate() if proc.returncode == 0: print(f'[{executable_path}]: Success') return stdout else: print(f'[{executable_path}]: Failed with error: {stderr.decode()}') return stderrasync def main(): executable = "path/to/your/executable.exe" # Example running the executable 3 times with different arguments tasks = [ run_executable(executable, ["arg1", "arg2"]), run_executable(executable, ["arg3", "arg4"]), run_executable(executable, ["arg5", "arg6"]), ] results = await asyncio.gather(*tasks)
for result in results: # Interpret the output in your way custom_output_handler(result.decode())def custom_output_handler(output): # Define how you want to handle/interpret the output here print(output)
if "name" == "main": asyncio.run(main())
Remove asyncio
- Assistant
To refactor your code to work without
asyncioand run processes synchronously instead, you'll need to use the module. Here’s how you can adapt your function to work synchronously: