0

I'm using fabric to run some tests, each test on each machine. I want to pass a unique index to each run invocation. When not allow duplicate hosts, I'm doing something like:

@task
@parallel
def run_test():
    idx = env.hosts.index(env.host)
    run('run_test %d' % (idx)

This works fine. However, now I want to run several tests on each machine. I do this by passing the same hostname several times and then setting env.dedupe_hosts = False, however, this breaks the above schema, since now each invocation will get the same index.

Is there any way to do this. I've tried to assign the index using multiprocess locker, but this did not work.

Cheers.

4

1 回答 1

0

[回答我自己的问题] 事实证明,使用多进程是解决这个问题的正确方法。您只需要使用 Lock 和 Value,例如:

def get_unique_index():
    env.DUP_LOCKER.acquire()
    result = env.UNIQUE_ID.value
    env.UNIQUE_ID.value = result + 1
    env.DUP_LOCKER.release()
    return result

你需要初始化的地方:

env.UNIQUE_ID.value = 0
env.DUP_LOCKER = Lock()
于 2013-06-12T08:19:41.283 回答