distributed icon indicating copy to clipboard operation
distributed copied to clipboard

pub/sub mechanism gets stuck

Open alexpashevich opened this issue 7 years ago • 3 comments

Hi,

I am using the pub/sub mechanism of dask and I can see that it often gets stuck right after being created. I have found that the order in which processes communicate with each other is important. At the moment I am using a hack with publishing Nones from the child processes however I find this solution unsatisfactory because I still can see the communication getting stuck. My library versions are:

dask==1.1.5
dask-jobqueue==0.4.1
distributed==1.26.1
tornado==5.1

Here are the two minimal examples. The first one always works for me, the second one gets stuck ~50% of the time. The only difference between them is the swapped lines 18-19 and 21-22.

The script that always works for me:

from dask.distributed import Client, LocalCluster, Pub, Sub

def create_proc(proc_idx):
    pub_out = Pub('counters')
    sub_in = Sub('proc{}_input'.format(int(proc_idx)))
    pub_out.put((0, proc_idx))
    for i, none_input in enumerate(sub_in):
        pub_out.put((i, proc_idx))

def main():
    num_processes = 2
    cluster = LocalCluster(n_workers=num_processes)
    client = Client(cluster)
    pub_out = [Pub('proc{}_input'.format(env_idx)) for env_idx in range(num_processes)]
    futures = client.map(create_proc, range(num_processes))
    sub_in = Sub('counters')

    for _ in range(num_processes):
        proc_counter, proc_idx = sub_in.get()

    for env_idx in range(num_processes):
        pub_out[env_idx].put(None)

    print('Main thread is waiting for the observations from envs')
    try:
        proc_counter, proc_idx = sub_in.get(timeout=5)
        print('Successfully executed the script')
    except Exception:
        print('TimeoutError')

if __name__ == '__main__':
    main()

The script that works ~50% of the time:

from dask.distributed import Client, LocalCluster, Pub, Sub

def create_proc(proc_idx):
    pub_out = Pub('counters')
    sub_in = Sub('proc{}_input'.format(int(proc_idx)))
    pub_out.put((0, proc_idx))
    for i, none_input in enumerate(sub_in):
        pub_out.put((i, proc_idx))

def main():
    num_processes = 2
    cluster = LocalCluster(n_workers=num_processes)
    client = Client(cluster)
    pub_out = [Pub('proc{}_input'.format(env_idx)) for env_idx in range(num_processes)]
    futures = client.map(create_proc, range(num_processes))
    sub_in = Sub('counters')

    for env_idx in range(num_processes):
        pub_out[env_idx].put(None)

    for _ in range(num_processes):
        proc_counter, proc_idx = sub_in.get()

    print('Main thread is waiting for the observations from envs')
    try:
        proc_counter, proc_idx = sub_in.get(timeout=5)
        print('Successfully executed the script')
    except Exception:
        print('TimeoutError')

if __name__ == '__main__':
    main()

Thank you for your help!

alexpashevich avatar May 16 '19 22:05 alexpashevich

Dask's PubSub unfortunately doesn't hold onto a history of recent messages, so if a Sub joins a topic after a Pub has already put something, the Sub won't get that message. If I add a sleep after you map your tasks out but before you publish on the Pub then everything works.

Some improvements that could help here:

  1. A backlog of recent messages kept somewhere (I don't know where this would be though)
  2. Some sort of coordination primitive like a shared semaphore or event
  3. The ability on pub/subs to wait until a specified number of peers have arrived (wouldn't be hard)

mrocklin avatar May 16 '19 22:05 mrocklin

@alexpashevich hopefully @mrocklin's comment about adding a sleep after you map your tasks works as a fix for the time being. Do you have an interest in working on any of the proposed improvements?

jrbourbeau avatar May 18 '19 17:05 jrbourbeau

Closing due to lack of response.

mrocklin avatar May 28 '19 15:05 mrocklin