瀏覽代碼

added param --cpu-limit

Fabian Peter Hammerle 7 年之前
父節點
當前提交
db7ee31582
共有 2 個文件被更改,包括 21 次插入3 次删除
  1. 11 1
      scripts/tooncher
  2. 10 2
      tooncher/__init__.py

+ 11 - 1
scripts/tooncher

@@ -7,7 +7,8 @@ import tooncher
 import yaml
 
 
-def run(username, config_path, engine_path=None, validate_ssl_certs=True):
+def run(username, config_path, engine_path=None, validate_ssl_certs=True,
+        cpu_limit_percent=None):
 
     if os.path.exists(config_path):
         with open(config_path) as f:
@@ -30,6 +31,7 @@ def run(username, config_path, engine_path=None, validate_ssl_certs=True):
                 username=account['username'],
                 password=account['password'],
                 validate_ssl_certs=validate_ssl_certs,
+                cpu_limit_percent=cpu_limit_percent,
             )
 
 
@@ -65,6 +67,14 @@ def _init_argparser():
         help='do not validate ssl certificates',
         action='store_false',
     )
+    argparser.add_argument(
+        '--cpu-limit',
+        dest='cpu_limit_percent',
+        type=int,
+        default=None,
+        help='maximally allowed cpu usage in percent'
+            + ' (requires cpulimit command, default: %(default)s)',
+    )
     return argparser
 
 

+ 10 - 2
tooncher/__init__.py

@@ -45,7 +45,7 @@ def start_engine(engine_path, gameserver, playcookie, **kwargs):
             'Frameworks',
         )
     elif sys.platform == 'linux' and 'XAUTHORITY' in os.environ:
-        """ 
+        """
         Fix for TTREngine reporting:
         > :display:x11display(error): Could not open display ":0.0".
         > :ToonBase: Default graphics pipe is glxGraphicsPipe (OpenGL).
@@ -121,7 +121,8 @@ def login(username=None, password=None,
         raise Exception(repr(resp_data))
 
 
-def launch(engine_path, username, password, validate_ssl_certs=True):
+def launch(engine_path, username, password, validate_ssl_certs=True,
+           cpu_limit_percent=None):
     result = login(
         username=username,
         password=password,
@@ -138,6 +139,13 @@ def launch(engine_path, username, password, validate_ssl_certs=True):
             gameserver=result.gameserver,
             playcookie=result.playcookie,
         )
+        if cpu_limit_percent is not None:
+            subprocess.Popen(args=[
+                'cpulimit',
+                '--pid', str(p.pid),
+                '--limit', str(cpu_limit_percent),
+                # '--verbose',
+            ])
         p.wait()
     else:
         raise Exception(repr(result))