在python中解析或运行docker shell命令

2024-10-16 20:43:03 发布

您现在位置:Python中文网/ 问答频道 /正文

以下面的shinyproxy山药为例:

proxy:
  title: Open Analytics Shiny Proxy
  logo-url: http://www.openanalytics.eu/sites/www.openanalytics.eu/themes/oa/logo.png
  landing-page: /
  heartbeat-rate: 10000
  heartbeat-timeout: 60000
  port: 8080
  authentication: simple
  admin-groups: scientists
  # Example: 'simple' authentication configuration
  users:
  - name: jack
    password: password
    groups: scientists
  - name: jeff
    password: password
    groups: mathematicians
  # Example: 'ldap' authentication configuration
  ldap:
    url: ldap://ldap.forumsys.com:389/dc=example,dc=com
    user-dn-pattern: uid={0}
    group-search-base:
    group-search-filter: (uniqueMember={0})
    manager-dn: cn=read-only-admin,dc=example,dc=com
    manager-password: password
  # Docker configuration
  docker:
    cert-path: /home/none
    url: http://localhost:2375
    port-range-start: 20000
  specs:
  - id: 01_hello
    display-name: Hello Application
    description: Application which demonstrates the basics of a Shiny app
    container-cmd: ["R", "-e", "shinyproxy::run_01_hello()"]
    container-image: openanalytics/shinyproxy-demo
    access-groups: [scientists, mathematicians]
  - id: 06_tabsets
    container-cmd: ["R", "-e", "shinyproxy::run_06_tabsets()"]
    container-image: openanalytics/shinyproxy-demo
    access-groups: scientists

logging:
  file:
    shinyproxy.log

如何将["R", "-e", "shinyproxy::run_06_tabsets()"]解析为普通的exec格式,或者直接运行此命令而不在python中进行解析?有空吗?在

编辑:

我想在subprocess中运行该命令,并在超时或运行Shiny app时发生错误后终止它:

MWE:

^{pr2}$

终端输出:

>>> from subprocess import Popen, PIPE
... from threading import Timer
... 
... cmd_from_yaml = ['R', '-e', 'shinyproxy::run_06_tabsets()']
... docker_cmd = "docker run -i {0} {1}".format(vol, img)
... docker_cmd = docker_cmd.split()
... docker_cmd.extend(cmd_from_yaml)
... timeout_sec = 10
... print("Running command: " + str(" ".join(docker_cmd)))
... proc = Popen(docker_cmd, stdout=PIPE, stderr=PIPE, shell=False)
... 
... timer = Timer(timeout_sec, proc.kill)
... try:
...     timer.start()
...     stdout, stderr = proc.communicate()
... finally:
...     timer.cancel()
... 
... if stderr:
...     proc.kill()
...     raise Exception("Error: " + str(stderr))
... 
Running command: docker run -i openanalytics/shinyproxy-demo R -e shinyproxy::run_06_tabsets()
Traceback (most recent call last):
  File "<input>", line 21, in <module>
Exception: Error: b'Loading required package: shiny\n\nListening on http://0.0.0.0:3838\n'

注意,命令docker run -i openanalytics/shinyproxy-demo R -e "shinyproxy::run_06_tabsets()"会因为添加缺少的引号而起作用,但我在这个问题中的重点是,是否有任何方法可以自动执行此操作,而不会像docker compose中那样手动添加缺少的引号。在


Tags: dockerrunfromcmddemocontainerpasswordproc