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.

eran
  • 6,731
  • 6
  • 35
  • 52

1 Answers1

0

[Answering my own question] It turned out that using multiprocess is the right solution for this. You just need to use Lock and Value, something like:

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

where you need to initialize:

env.UNIQUE_ID.value = 0
env.DUP_LOCKER = Lock()
eran
  • 6,731
  • 6
  • 35
  • 52