Skip to content

Commit a0660e9

Browse files
authored
Merge pull request #317 from trycua/fix/async_shell
Change computer.interface.run_command to be asynchronous
2 parents 333c187 + c61b131 commit a0660e9

File tree

3 files changed

+43
-15
lines changed

3 files changed

+43
-15
lines changed

libs/python/computer-server/computer_server/handlers/linux.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from typing import Dict, Any, List, Tuple, Optional
1111
import logging
1212
import subprocess
13+
import asyncio
1314
import base64
1415
import os
1516
import json
@@ -278,7 +279,20 @@ async def set_clipboard(self, text: str) -> Dict[str, Any]:
278279
# Command Execution
279280
async def run_command(self, command: str) -> Dict[str, Any]:
280281
try:
281-
process = subprocess.run(command, shell=True, capture_output=True, text=True)
282-
return {"success": True, "stdout": process.stdout, "stderr": process.stderr, "return_code": process.returncode}
282+
# Create subprocess
283+
process = await asyncio.create_subprocess_shell(
284+
command,
285+
stdout=asyncio.subprocess.PIPE,
286+
stderr=asyncio.subprocess.PIPE
287+
)
288+
# Wait for the subprocess to finish
289+
stdout, stderr = await process.communicate()
290+
# Return decoded output
291+
return {
292+
"success": True,
293+
"stdout": stdout.decode() if stdout else "",
294+
"stderr": stderr.decode() if stderr else "",
295+
"return_code": process.returncode
296+
}
283297
except Exception as e:
284298
return {"success": False, "error": str(e)}

libs/python/computer-server/computer_server/handlers/macos.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import re
4646
import json
4747
import copy
48+
import asyncio
4849
from .base import BaseAccessibilityHandler, BaseAutomationHandler
4950
import logging
5051

@@ -935,9 +936,20 @@ async def set_clipboard(self, text: str) -> Dict[str, Any]:
935936
async def run_command(self, command: str) -> Dict[str, Any]:
936937
"""Run a shell command and return its output."""
937938
try:
938-
import subprocess
939-
940-
process = subprocess.run(command, shell=True, capture_output=True, text=True)
941-
return {"success": True, "stdout": process.stdout, "stderr": process.stderr}
939+
# Create subprocess
940+
process = await asyncio.create_subprocess_shell(
941+
command,
942+
stdout=asyncio.subprocess.PIPE,
943+
stderr=asyncio.subprocess.PIPE
944+
)
945+
# Wait for the subprocess to finish
946+
stdout, stderr = await process.communicate()
947+
# Return decoded output
948+
return {
949+
"success": True,
950+
"stdout": stdout.decode() if stdout else "",
951+
"stderr": stderr.decode() if stderr else "",
952+
"return_code": process.returncode
953+
}
942954
except Exception as e:
943955
return {"success": False, "error": str(e)}

libs/python/computer-server/computer_server/handlers/windows.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from typing import Dict, Any, List, Tuple, Optional
88
import logging
99
import subprocess
10+
import asyncio
1011
import base64
1112
import os
1213
from io import BytesIO
@@ -387,18 +388,19 @@ async def set_clipboard(self, text: str) -> Dict[str, Any]:
387388
# Command Execution
388389
async def run_command(self, command: str) -> Dict[str, Any]:
389390
try:
390-
# Use cmd.exe for Windows commands
391-
process = subprocess.run(
392-
command,
393-
shell=True,
394-
capture_output=True,
395-
text=True,
396-
creationflags=subprocess.CREATE_NO_WINDOW if os.name == 'nt' else 0
391+
# Create subprocess
392+
process = await asyncio.create_subprocess_shell(
393+
command,
394+
stdout=asyncio.subprocess.PIPE,
395+
stderr=asyncio.subprocess.PIPE
397396
)
397+
# Wait for the subprocess to finish
398+
stdout, stderr = await process.communicate()
399+
# Return decoded output
398400
return {
399401
"success": True,
400-
"stdout": process.stdout,
401-
"stderr": process.stderr,
402+
"stdout": stdout.decode() if stdout else "",
403+
"stderr": stderr.decode() if stderr else "",
402404
"return_code": process.returncode
403405
}
404406
except Exception as e:

0 commit comments

Comments
 (0)