Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

common.py 1.4KB

123456789101112131415161718192021222324252627282930313233343536
  1. #
  2. # Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import asyncio
  17. from typing import Tuple
  18. async def async_run_command(*args, timeout: float = 5) -> Tuple[int, str, str]:
  19. """Safe asynchronous command execution tool"""
  20. proc = await asyncio.create_subprocess_exec(*args, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE)
  21. try:
  22. stdout, stderr = await asyncio.wait_for(proc.communicate(), timeout=timeout)
  23. if proc.returncode is None:
  24. raise RuntimeError("Process finished but returncode is None")
  25. return proc.returncode, stdout.decode(), stderr.decode()
  26. except asyncio.TimeoutError:
  27. proc.kill()
  28. await proc.wait()
  29. raise RuntimeError("Command timed out")
  30. except Exception as e:
  31. proc.kill()
  32. await proc.wait()
  33. raise e