【问题标题】:How to fetch python output and get to dynamic massive如何获取python输出并获得动态海量
【发布时间】:2014-05-29 14:22:52
【问题描述】:

我有 Jenkins 和 python,还有一些工作在排队。我有一个脚本

import jenkins 
j = jenkins.Jenkins('http:///', '....', '.......')
queue_info = j.get_queue_info()
print(queue_info)

这个命令给我以下输出:

[{u'task': {u'url': u'http://', u'color': u'aborted', u'name': u'f1111111111'}, u'stuck': False, u'url': 
u'queue/item/37/', u'inQueueSince': 1397554800875L, u'actions': [{u'causes': [{u'userName': u'admin', u'userId': u'admin', u'shortDescription':
u'Started by user admin'}]}], u'why': u'Waiting for next available executor on NO_1', u'buildable': True, u'params': u'', u'buildableStartMilliseconds': 1397554800878L, u'id': 37, u'pending': False, u'blocked': False}

如何在 python 中获取 u'name' 的名称并将其转换为动态海量

【问题讨论】:

    标签: python jenkins out massive


    【解决方案1】:

    输出是一个字典列表,因此您可以简单地访问列表中的第一个条目并使用字典的键。

    让我们从这里定义一个函数开始:

    import subprocess as sp
    
    ''' Runs a program '''
    def run(prog):
          print("Lets run program %s" % prog)
          sp.Popen([prog], stdout=sp.STDOUT, stderr=sp.STDOUT)
    
          # This makes the script wait until the end of the program
          sp.wait()
    

    那么,在for循环中,只需要调用run函数即可:

    for n in queue_info:
          run(n['task']['name'])
    

    【讨论】:

    • 我必须使用这个名字,因为我需要为这个队列运行虚拟机,所以我认为创建一个像job_name1 = 1,job_name2 = 2这样的大规模,然后运行是个好主意机器为1,2,3.....等等
    • 以及如何将参数添加到我的输出中?我的意思是这个 [0],如果我有很多工作?
    • 您可以简单地使用 for 循环来遍历数组。刚刚编辑了我的答案,看看吧。
    • 对不起,我是 python 新手,我按照你说的做了。 queue_info = j.get_queue_info() print(queue_info) for n in queue_info: print queue_info[n]['task']['name'] 并且有以下 TypeError: list indices must be integers, not dict
    • 感谢它的工作。也许你可以告诉我如何制作这个场景:对于每个 [n] ,调用 program(Maestro) 和 exec smth...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-11
    • 2014-11-01
    • 2013-09-27
    • 2011-07-26
    • 1970-01-01
    • 2018-05-17
    • 2023-02-24
    相关资源
    最近更新 更多