33# file, You can obtain one at http://mozilla.org/MPL/2.0/.
44import asyncio
55import logging
6+ import os
67import subprocess
78from typing import Any , Callable , Optional , Tuple , TypeVar , Union
89from typing_extensions import Protocol , runtime
910
1011from .sessions import Session
1112from .tasks import Corofunc
1213
13- __version__ = '0.1.0 '
14+ __version__ = '0.1.1 '
1415__all__ = ['run_shell' , 'run_process' , 'run_thread' ]
1516
1617log = logging .getLogger (__name__ )
@@ -33,14 +34,15 @@ def _scheduler() -> Optional[Scheduler]:
3334 return scheduler
3435
3536
36- async def run_shell (cmd : str , ** kwargs : Any ) -> ProcessOutput :
37+ async def run_shell (cmd : str , ncores : int = None , ** kwargs : Any ) -> ProcessOutput :
3738 """Execute a command in a shell.
3839
3940 Wrapper around :func:`asyncio.create_subprocess_shell` that handles errors
4041 and whose behavior can be modified by session plugins.
4142
4243 :param str cmd: a shell command to be executed
43- :param kwargs: all keyword arguments are passed to
44+ :param int ncores: number of cores that should be taken by the process
45+ :param kwargs: all other keyword arguments are passed to
4446 :func:`~asyncio.create_subprocess_shell`.
4547 :data:`~subprocess.PIPE` is passed to `stdin` and `stdout`
4648 keyword arguments by default.
@@ -56,14 +58,15 @@ async def run_shell(cmd: str, **kwargs: Any) -> ProcessOutput:
5658 return await _run_process (cmd , ** kwargs )
5759
5860
59- async def run_process (* args : str , ** kwargs : Any ) -> ProcessOutput :
61+ async def run_process (* args : str , ncores : int = None , ** kwargs : Any ) -> ProcessOutput :
6062 """Create a subprocess.
6163
6264 Wrapper around :func:`asyncio.create_subprocess_exec` that handles errors
6365 and whose behavior can be modified by session plugins.
6466
6567 :param str args: arguments of the subprocess
66- :param kwargs: all keyword arguments are passed to
68+ :param int ncores: number of cores that should be taken by the process
69+ :param kwargs: all other keyword arguments are passed to
6770 :func:`~asyncio.create_subprocess_exec`.
6871 :data:`~subprocess.PIPE` is passed to `stdin` and `stdout`
6972 keyword arguments by default.
@@ -73,18 +76,22 @@ async def run_process(*args: str, **kwargs: Any) -> ProcessOutput:
7376 """
7477 scheduler = _scheduler ()
7578 if scheduler :
76- return await scheduler (_run_process , args , ** kwargs )
79+ return await scheduler (_run_process , args , ncores = ncores , ** kwargs )
7780 return await _run_process (args , ** kwargs )
7881
7982
8083async def _run_process (
8184 args : Union [str , Tuple [str , ...]],
8285 shell : bool = False ,
8386 input : bytes = None ,
87+ ncores : int = None ,
8488 ** kwargs : Any ,
8589) -> Union [bytes , Tuple [bytes , bytes ]]:
8690 kwargs .setdefault ('stdin' , subprocess .PIPE )
8791 kwargs .setdefault ('stdout' , subprocess .PIPE )
92+ kwargs .setdefault ('env' , os .environ .copy ())
93+ if ncores is not None :
94+ kwargs ['env' ]['MONA_NCORES' ] = str (ncores )
8895 if shell :
8996 assert isinstance (args , str )
9097 proc = await asyncio .create_subprocess_shell (args , ** kwargs )
0 commit comments