Skip to content

Commit

Permalink
Fixed bugs when running aiaccel in local mode on Windows (#370)
Browse files Browse the repository at this point in the history
* added descriptions wrt Windows environment.

* added select_for_win() in process.py.
*  fixed Popen in local_model.py.
* fixed descriptions wrt Windows in docs.

* added docstrings wrt select_for_win() in process.py.

* fixed type hint of select_for_win().

* fixed type hint.

* fixed type hint

* fixed type hint

* fixed type hint.

* fixed format.

---------

Co-authored-by: Yoshiaki Bando <[email protected]>
  • Loading branch information
3bisuoka and yoshipon authored Jun 10, 2024
1 parent a9b8a17 commit 5d42b40
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 6 deletions.
8 changes: 6 additions & 2 deletions aiaccel/manager/job/model/local_model.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import re
import sys
from subprocess import PIPE, Popen
from typing import TYPE_CHECKING, Any

Expand Down Expand Up @@ -33,7 +34,10 @@ def job_submitted(self, obj: Job) -> None:
obj.config.generic.enabled_variable_name_argumentation,
)
obj.logger.info(f'runner command: {" ".join(runner_command)}')
obj.proc = Popen(runner_command, stdout=PIPE, stderr=PIPE, bufsize=0)
if sys.platform == "win32":
obj.proc = Popen(runner_command, stdout=PIPE, stderr=PIPE, bufsize=0, shell=True)
else:
obj.proc = Popen(runner_command, stdout=PIPE, stderr=PIPE, bufsize=0)

obj.th_oh = OutputHandler(obj.proc)
obj.th_oh.start()
Expand Down Expand Up @@ -177,5 +181,5 @@ def write_results_to_database(self, obj: "Job") -> None:
commands.append(f'--{param["name"]}={param["value"]}')

obj.logger.debug(f'{" ".join(commands)}')
Popen(commands)
Popen(commands, shell=True) if sys.platform == "win32" else Popen(commands)
return None
40 changes: 39 additions & 1 deletion aiaccel/util/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,49 @@

import copy
import datetime
import os
import select
import subprocess
import sys
import threading
import time
from typing import IO, Any

from aiaccel.common import datetime_format


def select_for_win(rlist: list[IO[bytes]], timeout: int = 1) -> list[IO[bytes]] | Any:
"""Alternative to select.select() on Windows.
Args:
rlist (list): A list of IO objects. It waits until ready for reading.
timeout (int): An integer specifies a time-out in seconds.
Returns:
tuple[list]: A tuple consisting a list of readable objects
and Exceptions.
"""
start_time = time.time()
readable, errorlist = [], []

while True:
current_time = time.time()
elapsed_time = current_time - start_time
if timeout is not None and elapsed_time >= timeout:
break
for fd_r in rlist:
try:
if os.read(fd_r.fileno(), 1):
readable.append(fd_r)
except BlockingIOError:
pass
except Exception:
errorlist.append(fd_r)
if readable or errorlist:
break
return readable, errorlist


class OutputHandler(threading.Thread):
"""A class to print subprocess outputs.
Expand Down Expand Up @@ -64,7 +99,10 @@ def run(self) -> None:
self._start_time = datetime.datetime.now()
while True:
inputs = [self._proc.stdout, self._proc.stderr]
readable, _, _ = select.select(inputs, [], [], self._sleep_time)
if sys.platform == "win32":
readable, _ = select_for_win(inputs, self._sleep_time)
else:
readable, _, _ = select.select(inputs, [], [], self._sleep_time)
for s in readable:
line = s.readline()
if s is self._proc.stdout and line:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ generic:
- **logging_level** - ログの出力レベルを `"INFO"` に設定します.
> Windows では,仮想環境の python で実行するためには `job_command` の欄を `"optenv/Scripts/python.exe"` のように設定する必要があります
> Windows 上で仮想環境の python プログラムを実行するために `job_command` の欄を `"path\\to\\optenv\\Scripts\\python.exe user.py"` と設定する必要があります.`"path\\to\\"` の部分はご自身の環境のパスを絶対パスで指定してください


**resource**
Expand Down
2 changes: 1 addition & 1 deletion docs/source/examples/local_random.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ generic:
- **logging_level** - ログの出力レベルを `"INFO"` に設定します.
> Windows 上で仮想環境の python プログラムを実行するために `job_command` の欄を `"optenv/Scripts/python.exe"` と設定する必要があります.
> Windows 上で仮想環境の python プログラムを実行するために `job_command` の欄を `"path\\to\\optenv\\Scripts\\python.exe user.py"` と設定する必要があります.`"path\\to\\"` の部分はご自身の環境のパスを絶対パスで指定してください


#### resource
Expand Down
3 changes: 2 additions & 1 deletion docs/source/installation/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,8 @@ deactivate


### インストール

#### (注意)Windowsセキュリティによるコンテンツブロック
お使いのアンチウイルスソフト(Defender等)の設定によっては先ほど作成した仮想環境上での pip コマンドや Python コマンド等の操作がブロックされる可能性があります.
```{note}
事前に pip をアップグレードすることを推奨いたします.
Expand Down

0 comments on commit 5d42b40

Please sign in to comment.