Repository: dask/dask

Issue:
dask.bag.count hangs on distributed scheduler.
Whenever a dask.bag.count item is computed with the distributed scheduler it hangs.



Discussion:
This only happens with the distributed scheduler.

``` python
In [1]: from IPython.parallel import Client

In [2]: import dask

In [3]: import dask.bag as db

In [4]: from dask.distributed import dask_client_from_ipclient

In [5]: c = dask_client_from_ipclient(Client())

In [6]: dask.set_options(get=c.get)
Out[6]: <dask.context.set_options at 0x7f81fb8d8250>

In [7]: db.from_s3('mtg-sets', 'A*-x.json')
Out[7]: <dask.bag.core.Bag at 0x7f81fa0e7d50>

In [8]: x = db.from_s3('mtg-sets', 'A*-x.json')

In [9]: x.count().compute()
```

Here are the logs

client

```
client-2ccde40a-0585-11e5-adba-34363b801fb8 Send to scheduler {'function': 'schedule', 'jobid': 'schedule-0'}
```

scheduler

```
tcp://camargo:43659 Receive job from worker {'function': 'register', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 4, 486387), 'address': 'tcp://camargo:60716'}
tcp://camargo:43659 Receive job from worker {'function': 'register', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 4, 487810), 'address': 'tcp://camargo:40314'}
tcp://camargo:43659 Receive job from worker {'function': 'register', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 4, 498889), 'address': 'tcp://camargo:41341'}
tcp://camargo:43659 Receive job from worker {'function': 'register', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 4, 506664), 'address': 'tcp://camargo:49982'}
tcp://camargo:43659 Receive job from worker {'function': 'register', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 4, 507769), 'address': 'tcp://camargo:40401'}
tcp://camargo:43659 Receive job from worker {'function': 'register', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 4, 509760), 'address': 'tcp://camargo:41358'}
tcp://camargo:43659 Receive job from worker {'function': 'register', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 4, 512215), 'address': 'tcp://camargo:47955'}
tcp://camargo:34272 Receive job from client {'function': 'schedule', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 53, 553051), 'loads': <function loads at 0x7fbb940b72a8>, 'jobid': 'schedule-0', 'address': 'client-2ccde40a-0585-11e5-adba-34363b801fb8'}
tcp://camargo:43659 Scheduling dask
tcp://camargo:43659 Send to worker tcp://camargo:40314 {'function': 'compute', 'dumps': <function dumps at 0x7fbb940b71b8>, 'loads': <function loads at 0x7fbb940b72a8>, 'jobid': ('bag-1', 8)}
tcp://camargo:43659 Send to worker tcp://camargo:41341 {'function': 'compute', 'dumps': <function dumps at 0x7fbb940b71b8>, 'loads': <function loads at 0x7fbb940b72a8>, 'jobid': ('bag-1', 7)}
tcp://camargo:43659 Send to worker tcp://camargo:49982 {'function': 'compute', 'dumps': <function dumps at 0x7fbb940b71b8>, 'loads': <function loads at 0x7fbb940b72a8>, 'jobid': ('bag-1', 6)}
tcp://camargo:43659 Send to worker tcp://camargo:40401 {'function': 'compute', 'dumps': <function dumps at 0x7fbb940b71b8>, 'loads': <function loads at 0x7fbb940b72a8>, 'jobid': ('bag-1', 5)}
tcp://camargo:43659 Send to worker tcp://camargo:41358 {'function': 'compute', 'dumps': <function dumps at 0x7fbb940b71b8>, 'loads': <function loads at 0x7fbb940b72a8>, 'jobid': ('bag-1', 4)}
tcp://camargo:43659 Send to worker tcp://camargo:47955 {'function': 'compute', 'dumps': <function dumps at 0x7fbb940b71b8>, 'loads': <function loads at 0x7fbb940b72a8>, 'jobid': ('bag-1', 3)}
tcp://camargo:43659 Send to worker tcp://camargo:60716 {'function': 'compute', 'dumps': <function dumps at 0x7fbb940b71b8>, 'loads': <function loads at 0x7fbb940b72a8>, 'jobid': ('bag-1', 2)}
tcp://camargo:43659 Receive job from worker {'function': 'finished-task', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 54, 283752), 'address': 'tcp://camargo:49982'}
tcp://camargo:43659 Finish task {'duration': 0.6064548492431641, 'status': 'OK', 'dependencies': [], 'queue': '4a0f3564-0585-11e5-beb0-34363b801fb8', 'key': ('bag-1', 6)}
tcp://camargo:43659 Send to worker tcp://camargo:49982 {'function': 'compute', 'dumps': <function dumps at 0x7fbb940b71b8>, 'loads': <function loads at 0x7fbb940b72a8>, 'jobid': ('bag-1', 1)}
tcp://camargo:43659 Receive job from worker {'function': 'finished-task', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 54, 384253), 'address': 'tcp://camargo:40401'}
tcp://camargo:43659 Finish task {'duration': 0.6480710506439209, 'status': 'OK', 'dependencies': [], 'queue': '4a0f3564-0585-11e5-beb0-34363b801fb8', 'key': ('bag-1', 5)}
tcp://camargo:43659 Send to worker tcp://camargo:40401 {'function': 'compute', 'dumps': <function dumps at 0x7fbb940b71b8>, 'loads': <function loads at 0x7fbb940b72a8>, 'jobid': ('bag-1', 0)}
tcp://camargo:43659 Receive job from worker {'function': 'finished-task', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 54, 526744), 'address': 'tcp://camargo:60716'}
tcp://camargo:43659 Finish task {'duration': 0.790877103805542, 'status': 'OK', 'dependencies': [], 'queue': '4a0f3564-0585-11e5-beb0-34363b801fb8', 'key': ('bag-1', 2)}
tcp://camargo:43659 Receive job from worker {'function': 'finished-task', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 54, 548605), 'address': 'tcp://camargo:40314'}
tcp://camargo:43659 Finish task {'duration': 0.8616559505462646, 'status': 'OK', 'dependencies': [], 'queue': '4a0f3564-0585-11e5-beb0-34363b801fb8', 'key': ('bag-1', 8)}
tcp://camargo:43659 Receive job from worker {'function': 'finished-task', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 54, 593253), 'address': 'tcp://camargo:41358'}
tcp://camargo:43659 Finish task {'duration': 0.8623490333557129, 'status': 'OK', 'dependencies': [], 'queue': '4a0f3564-0585-11e5-beb0-34363b801fb8', 'key': ('bag-1', 4)}
tcp://camargo:43659 Receive job from worker {'function': 'finished-task', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 54, 736411), 'address': 'tcp://camargo:47955'}
tcp://camargo:43659 Finish task {'duration': 0.9897041320800781, 'status': 'OK', 'dependencies': [], 'queue': '4a0f3564-0585-11e5-beb0-34363b801fb8', 'key': ('bag-1', 3)}
tcp://camargo:43659 Receive job from worker {'function': 'finished-task', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 185222), 'address': 'tcp://camargo:41341'}
tcp://camargo:43659 Finish task {'duration': 1.510915994644165, 'status': 'OK', 'dependencies': [], 'queue': '4a0f3564-0585-11e5-beb0-34363b801fb8', 'key': ('bag-1', 7)}
tcp://camargo:43659 Receive job from worker {'function': 'finished-task', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 290983), 'address': 'tcp://camargo:49982'}
tcp://camargo:43659 Finish task {'duration': 1.0013291835784912, 'status': 'OK', 'dependencies': [], 'queue': '4a0f3564-0585-11e5-beb0-34363b801fb8', 'key': ('bag-1', 1)}
tcp://camargo:43659 Receive job from worker {'function': 'finished-task', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 523312), 'address': 'tcp://camargo:40401'}
tcp://camargo:43659 Finish task {'duration': 1.1346118450164795, 'status': 'OK', 'dependencies': [], 'queue': '4a0f3564-0585-11e5-beb0-34363b801fb8', 'key': ('bag-1', 0)}
tcp://camargo:43659 Send to worker tcp://camargo:60716 {'function': 'compute', 'dumps': <function dumps at 0x7fbb940b71b8>, 'loads': <function loads at 0x7fbb940b72a8>, 'jobid': 'bag-2'}
tcp://camargo:43659 Receive job from worker {'function': 'finished-task', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 530821), 'address': 'tcp://camargo:60716'}
tcp://camargo:43659 Finish task {'duration': 6.794929504394531e-05, 'status': 'OK', 'dependencies': [('bag-1', 4), ('bag-1', 5), ('bag-1', 6), ('bag-1', 7), ('bag-1', 0), ('bag-1', 1), ('bag-1', 2), ('bag-1', 8), ('bag-1', 3)], 'queue': '4a0f3564-0585-11e5-beb0-34363b801fb8', 'key': 'bag-2'}
tcp://camargo:43659 Release data ('bag-1', 4) ['tcp://camargo:60716', 'tcp://camargo:41358']
tcp://camargo:43659 Send to worker tcp://camargo:60716 {'function': 'delitem', 'jobid': ('bag-1', 4)}
tcp://camargo:43659 Send to worker tcp://camargo:41358 {'function': 'delitem', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 531439), 'address': 'tcp://camargo:43659', 'jobid': ('bag-1', 4)}
tcp://camargo:43659 Release data ('bag-1', 5) ['tcp://camargo:60716', 'tcp://camargo:40401']
tcp://camargo:43659 Send to worker tcp://camargo:60716 {'function': 'delitem', 'jobid': ('bag-1', 5)}
tcp://camargo:43659 Send to worker tcp://camargo:40401 {'function': 'delitem', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 531801), 'address': 'tcp://camargo:43659', 'jobid': ('bag-1', 5)}
tcp://camargo:43659 Release data ('bag-1', 6) ['tcp://camargo:60716', 'tcp://camargo:49982']
tcp://camargo:43659 Send to worker tcp://camargo:60716 {'function': 'delitem', 'jobid': ('bag-1', 6)}
tcp://camargo:43659 Send to worker tcp://camargo:49982 {'function': 'delitem', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 532182), 'address': 'tcp://camargo:43659', 'jobid': ('bag-1', 6)}
tcp://camargo:43659 Release data ('bag-1', 7) ['tcp://camargo:60716', 'tcp://camargo:41341']
tcp://camargo:43659 Send to worker tcp://camargo:60716 {'function': 'delitem', 'jobid': ('bag-1', 7)}
tcp://camargo:43659 Send to worker tcp://camargo:41341 {'function': 'delitem', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 532973), 'address': 'tcp://camargo:43659', 'jobid': ('bag-1', 7)}
tcp://camargo:43659 Release data ('bag-1', 0) ['tcp://camargo:60716', 'tcp://camargo:40401']
tcp://camargo:43659 Send to worker tcp://camargo:60716 {'function': 'delitem', 'jobid': ('bag-1', 0)}
tcp://camargo:43659 Send to worker tcp://camargo:40401 {'function': 'delitem', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 533363), 'address': 'tcp://camargo:43659', 'jobid': ('bag-1', 0)}
tcp://camargo:43659 Release data ('bag-1', 1) ['tcp://camargo:60716', 'tcp://camargo:49982']
tcp://camargo:43659 Send to worker tcp://camargo:60716 {'function': 'delitem', 'jobid': ('bag-1', 1)}
tcp://camargo:43659 Send to worker tcp://camargo:49982 {'function': 'delitem', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 533955), 'address': 'tcp://camargo:43659', 'jobid': ('bag-1', 1)}
tcp://camargo:43659 Release data ('bag-1', 2) ['tcp://camargo:60716']
tcp://camargo:43659 Send to worker tcp://camargo:60716 {'function': 'delitem', 'jobid': ('bag-1', 2)}
tcp://camargo:43659 Release data ('bag-1', 8) ['tcp://camargo:40314', 'tcp://camargo:60716']
tcp://camargo:43659 Send to worker tcp://camargo:40314 {'function': 'delitem', 'jobid': ('bag-1', 8)}
tcp://camargo:43659 Send to worker tcp://camargo:60716 {'function': 'delitem', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 534599), 'address': 'tcp://camargo:43659', 'jobid': ('bag-1', 8)}
tcp://camargo:43659 Release data ('bag-1', 3) ['tcp://camargo:47955', 'tcp://camargo:60716']
tcp://camargo:43659 Send to worker tcp://camargo:47955 {'function': 'delitem', 'jobid': ('bag-1', 3)}
tcp://camargo:43659 Send to worker tcp://camargo:60716 {'function': 'delitem', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 535043), 'address': 'tcp://camargo:43659', 'jobid': ('bag-1', 3)}
tcp://camargo:43659 Send to worker tcp://camargo:60716 {'function': 'getitem', 'jobid': 'bag-2'}
tcp://camargo:43659 Receive job from worker {'status': 'OK', 'function': 'getitem-ack', 'jobid': 'bag-2', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 536191), 'address': 'tcp://camargo:60716'}
tcp://camargo:43659 Getitem ack {'queue': '4b370462-0585-11e5-beb0-34363b801fb8', 'key': 'bag-2', 'value': 9}
```

workers

```
Hello from worker.py
tcp://camargo:60716 Send to scheduler {'function': 'register'}
tcp://camargo:60716 Start up tcp://camargo:43659
tcp://camargo:40314 Send to scheduler {'function': 'register'}
tcp://camargo:40314 Start up tcp://camargo:43659
tcp://camargo:41341 Send to scheduler {'function': 'register'}
tcp://camargo:41341 Start up tcp://camargo:43659
tcp://camargo:49982 Send to scheduler {'function': 'register'}
tcp://camargo:49982 Start up tcp://camargo:43659
tcp://camargo:40401 Send to scheduler {'function': 'register'}
tcp://camargo:40401 Start up tcp://camargo:43659
tcp://camargo:41358 Send to scheduler {'function': 'register'}
tcp://camargo:41358 Start up tcp://camargo:43659
tcp://camargo:47955 Send to scheduler {'function': 'register'}
tcp://camargo:47955 Start up tcp://camargo:43659
tcp://camargo:40314 Receive job from scheduler {'function': 'compute', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 53, 596936), 'jobid': ('bag-1', 8), 'dumps': <function dumps at 0x7f50ad4851b8>, 'address': 'tcp://camargo:43659', 'loads': <function loads at 0x7f50ad4852a8>}
tcp://camargo:41341 Receive job from scheduler {'function': 'compute', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 53, 598478), 'jobid': ('bag-1', 7), 'dumps': <function dumps at 0x7fd52a8d61b8>, 'address': 'tcp://camargo:43659', 'loads': <function loads at 0x7fd52a8d62a8>}
tcp://camargo:49982 Receive job from scheduler {'function': 'compute', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 53, 599932), 'jobid': ('bag-1', 6), 'dumps': <function dumps at 0x7f14cc10b1b8>, 'address': 'tcp://camargo:43659', 'loads': <function loads at 0x7f14cc10b2a8>}
tcp://camargo:40401 Receive job from scheduler {'function': 'compute', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 53, 602512), 'jobid': ('bag-1', 5), 'dumps': <function dumps at 0x7f187e70f1b8>, 'address': 'tcp://camargo:43659', 'loads': <function loads at 0x7f187e70f2a8>}
tcp://camargo:47955 Receive job from scheduler {'function': 'compute', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 53, 607469), 'jobid': ('bag-1', 3), 'dumps': <function dumps at 0x7fa0380df1b8>, 'address': 'tcp://camargo:43659', 'loads': <function loads at 0x7fa0380df2a8>}
tcp://camargo:60716 Receive job from scheduler {'function': 'compute', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 53, 626957), 'jobid': ('bag-1', 2), 'dumps': <function dumps at 0x7f688de3b1b8>, 'address': 'tcp://camargo:43659', 'loads': <function loads at 0x7f688de3b2a8>}
tcp://camargo:41358 Receive job from scheduler {'function': 'compute', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 53, 604913), 'jobid': ('bag-1', 4), 'dumps': <function dumps at 0x7fb8f8d1e1b8>, 'address': 'tcp://camargo:43659', 'loads': <function loads at 0x7fb8f8d1e2a8>}
tcp://camargo:41341 Start computation ('bag-1', 7) (<function count at 0x7fd52a88e9b0>, (<function <lambda> at 0x7fd5100948c0>, (<bound method Bucket.get_key of <Bucket: mtg-sets>>, 'ATQ-x.json')))
tcp://camargo:49982 Start computation ('bag-1', 6) (<function count at 0x7f14cc0c39b0>, (<function <lambda> at 0x7f14bc1038c0>, (<bound method Bucket.get_key of <Bucket: mtg-sets>>, 'ATH-x.json')))
tcp://camargo:40314 Start computation ('bag-1', 8) (<function count at 0x7f50ad43d9b0>, (<function <lambda> at 0x7f50ac07f8c0>, (<bound method Bucket.get_key of <Bucket: mtg-sets>>, 'AVR-x.json')))
tcp://camargo:41358 Start computation ('bag-1', 4) (<function count at 0x7fb8f8cd69b0>, (<function <lambda> at 0x7fb8f81198c0>, (<bound method Bucket.get_key of <Bucket: mtg-sets>>, 'ARC-x.json')))
tcp://camargo:60716 Start computation ('bag-1', 2) (<function count at 0x7f688ddf39b0>, (<function <lambda> at 0x7f688c01c8c0>, (<bound method Bucket.get_key of <Bucket: mtg-sets>>, 'APC-x.json')))
tcp://camargo:40401 Start computation ('bag-1', 5) (<function count at 0x7f187e6c79b0>, (<function <lambda> at 0x7f187c0ef8c0>, (<bound method Bucket.get_key of <Bucket: mtg-sets>>, 'ARN-x.json')))
tcp://camargo:47955 Start computation ('bag-1', 3) (<function count at 0x7fa0380979b0>, (<function <lambda> at 0x7fa0101148c0>, (<bound method Bucket.get_key of <Bucket: mtg-sets>>, 'ARB-x.json')))
tcp://camargo:49982 End computation ('bag-1', 6) (<function count at 0x7f14cc0c39b0>, (<function <lambda> at 0x7f14bc1038c0>, (<bound method Bucket.get_key of <Bucket: mtg-sets>>, 'ATH-x.json'))) OK
tcp://camargo:49982 Send to scheduler {'function': 'finished-task'}
tcp://camargo:49982 Receive job from scheduler {'function': 'compute', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 54, 284666), 'jobid': ('bag-1', 1), 'dumps': <function dumps at 0x7f14cc10b1b8>, 'address': 'tcp://camargo:43659', 'loads': <function loads at 0x7f14cc10b2a8>}
tcp://camargo:49982 Start computation ('bag-1', 1) (<function count at 0x7f14cc0c39b0>, (<function <lambda> at 0x7f14bc05ced8>, (<bound method Bucket.get_key of <Bucket: mtg-sets>>, 'ALL-x.json')))
tcp://camargo:40401 End computation ('bag-1', 5) (<function count at 0x7f187e6c79b0>, (<function <lambda> at 0x7f187c0ef8c0>, (<bound method Bucket.get_key of <Bucket: mtg-sets>>, 'ARN-x.json'))) OK
tcp://camargo:40401 Send to scheduler {'function': 'finished-task'}
tcp://camargo:40401 Receive job from scheduler {'function': 'compute', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 54, 385112), 'jobid': ('bag-1', 0), 'dumps': <function dumps at 0x7f187e70f1b8>, 'address': 'tcp://camargo:43659', 'loads': <function loads at 0x7f187e70f2a8>}
tcp://camargo:40401 Start computation ('bag-1', 0) (<function count at 0x7f187e6c79b0>, (<function <lambda> at 0x7f187c048ed8>, (<bound method Bucket.get_key of <Bucket: mtg-sets>>, 'ALA-x.json')))
tcp://camargo:60716 End computation ('bag-1', 2) (<function count at 0x7f688ddf39b0>, (<function <lambda> at 0x7f688c01c8c0>, (<bound method Bucket.get_key of <Bucket: mtg-sets>>, 'APC-x.json'))) OK
tcp://camargo:60716 Send to scheduler {'function': 'finished-task'}
tcp://camargo:40314 End computation ('bag-1', 8) (<function count at 0x7f50ad43d9b0>, (<function <lambda> at 0x7f50ac07f8c0>, (<bound method Bucket.get_key of <Bucket: mtg-sets>>, 'AVR-x.json'))) OK
tcp://camargo:40314 Send to scheduler {'function': 'finished-task'}
tcp://camargo:41358 End computation ('bag-1', 4) (<function count at 0x7fb8f8cd69b0>, (<function <lambda> at 0x7fb8f81198c0>, (<bound method Bucket.get_key of <Bucket: mtg-sets>>, 'ARC-x.json'))) OK
tcp://camargo:41358 Send to scheduler {'function': 'finished-task'}
tcp://camargo:47955 End computation ('bag-1', 3) (<function count at 0x7fa0380979b0>, (<function <lambda> at 0x7fa0101148c0>, (<bound method Bucket.get_key of <Bucket: mtg-sets>>, 'ARB-x.json'))) OK
tcp://camargo:47955 Send to scheduler {'function': 'finished-task'}
tcp://camargo:41341 End computation ('bag-1', 7) (<function count at 0x7fd52a88e9b0>, (<function <lambda> at 0x7fd5100948c0>, (<bound method Bucket.get_key of <Bucket: mtg-sets>>, 'ATQ-x.json'))) OK
tcp://camargo:41341 Send to scheduler {'function': 'finished-task'}
tcp://camargo:49982 End computation ('bag-1', 1) (<function count at 0x7f14cc0c39b0>, (<function <lambda> at 0x7f14bc05ced8>, (<bound method Bucket.get_key of <Bucket: mtg-sets>>, 'ALL-x.json'))) OK
tcp://camargo:49982 Send to scheduler {'function': 'finished-task'}
tcp://camargo:40401 End computation ('bag-1', 0) (<function count at 0x7f187e6c79b0>, (<function <lambda> at 0x7f187c048ed8>, (<bound method Bucket.get_key of <Bucket: mtg-sets>>, 'ALA-x.json'))) OK
tcp://camargo:40401 Send to scheduler {'function': 'finished-task'}
tcp://camargo:60716 Receive job from scheduler {'function': 'compute', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 524055), 'jobid': 'bag-2', 'dumps': <function dumps at 0x7f688de3b1b8>, 'address': 'tcp://camargo:43659', 'loads': <function loads at 0x7f688de3b2a8>}
tcp://camargo:60716 Collect data from peers {('bag-1', 4): set(['tcp://camargo:41358']), ('bag-1', 5): set(['tcp://camargo:40401']), ('bag-1', 6): set(['tcp://camargo:49982']), ('bag-1', 7): set(['tcp://camargo:41341']), ('bag-1', 0): set(['tcp://camargo:40401']), ('bag-1', 1): set(['tcp://camargo:49982']), ('bag-1', 2): set(['tcp://camargo:60716']), ('bag-1', 8): set(['tcp://camargo:40314']), ('bag-1', 3): set(['tcp://camargo:47955'])}
tcp://camargo:60716 Send to worker tcp://camargo:41358 {'function': 'getitem', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 525643), 'address': 'tcp://camargo:60716', 'jobid': ('bag-1', 4)}
tcp://camargo:60716 Send to worker tcp://camargo:40401 {'function': 'getitem', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 525853), 'address': 'tcp://camargo:60716', 'jobid': ('bag-1', 5)}
tcp://camargo:41358 Receive job from worker \00\C3ܟ\C4 {'function': 'getitem', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 525643), 'jobid': ('bag-1', 4), 'address': 'tcp://camargo:60716'}
tcp://camargo:60716 Send to worker tcp://camargo:49982 {'function': 'getitem', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 526052), 'address': 'tcp://camargo:60716', 'jobid': ('bag-1', 6)}
tcp://camargo:40401 Receive job from worker \00s\ED\E8 {'function': 'getitem', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 525853), 'jobid': ('bag-1', 5), 'address': 'tcp://camargo:60716'}
tcp://camargo:60716 Send to worker tcp://camargo:41341 {'function': 'getitem', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 526218), 'address': 'tcp://camargo:60716', 'jobid': ('bag-1', 7)}
tcp://camargo:60716 Send to worker tcp://camargo:40401 {'function': 'getitem', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 526304), 'address': 'tcp://camargo:60716', 'jobid': ('bag-1', 0)}
tcp://camargo:40401 Getitem for worker {'function': 'getitem', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 525853), 'jobid': ('bag-1', 5), 'address': 'tcp://camargo:60716'} {'function': 'getitem', 'queue': '4b35789a-0585-11e5-9ab0-34363b801fb8', 'key': ('bag-1', 5)}
tcp://camargo:60716 Send to worker tcp://camargo:49982 {'function': 'getitem', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 526399), 'address': 'tcp://camargo:60716', 'jobid': ('bag-1', 1)}
tcp://camargo:49982 Receive job from worker \00fZ}\AD {'function': 'getitem', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 526052), 'jobid': ('bag-1', 6), 'address': 'tcp://camargo:60716'}
tcp://camargo:40401 Send to worker tcp://camargo:60716 {'function': 'getitem-ack', 'status': 'OK', 'address': 'tcp://camargo:40401', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 526525), 'jobid': ('bag-1', 5)}
tcp://camargo:41341 Receive job from worker \00Ї\9CZ {'function': 'getitem', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 526218), 'jobid': ('bag-1', 7), 'address': 'tcp://camargo:60716'}
tcp://camargo:60716 Send to worker tcp://camargo:40314 {'function': 'getitem', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 526580), 'address': 'tcp://camargo:60716', 'jobid': ('bag-1', 8)}
tcp://camargo:49982 Getitem for worker {'function': 'getitem', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 526052), 'jobid': ('bag-1', 6), 'address': 'tcp://camargo:60716'} {'function': 'getitem', 'queue': '4b35789a-0585-11e5-9ab0-34363b801fb8', 'key': ('bag-1', 6)}
tcp://camargo:60716 Send to worker tcp://camargo:47955 {'function': 'getitem', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 526745), 'address': 'tcp://camargo:60716', 'jobid': ('bag-1', 3)}
tcp://camargo:49982 Send to worker tcp://camargo:60716 {'function': 'getitem-ack', 'status': 'OK', 'address': 'tcp://camargo:49982', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 526836), 'jobid': ('bag-1', 6)}
tcp://camargo:40401 Receive job from worker \00s\ED\E8 {'function': 'getitem', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 526304), 'jobid': ('bag-1', 0), 'address': 'tcp://camargo:60716'}
tcp://camargo:41358 Getitem for worker {'function': 'getitem', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 525643), 'jobid': ('bag-1', 4), 'address': 'tcp://camargo:60716'} {'function': 'getitem', 'queue': '4b35789a-0585-11e5-9ab0-34363b801fb8', 'key': ('bag-1', 4)}
tcp://camargo:41341 Getitem for worker {'function': 'getitem', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 526218), 'jobid': ('bag-1', 7), 'address': 'tcp://camargo:60716'} {'function': 'getitem', 'queue': '4b35789a-0585-11e5-9ab0-34363b801fb8', 'key': ('bag-1', 7)}
tcp://camargo:41358 Send to worker tcp://camargo:60716 {'function': 'getitem-ack', 'status': 'OK', 'address': 'tcp://camargo:41358', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 527164), 'jobid': ('bag-1', 4)}
tcp://camargo:41341 Send to worker tcp://camargo:60716 {'function': 'getitem-ack', 'status': 'OK', 'address': 'tcp://camargo:41341', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 527348), 'jobid': ('bag-1', 7)}
tcp://camargo:40314 Receive job from worker \00(\8F {'function': 'getitem', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 526580), 'jobid': ('bag-1', 8), 'address': 'tcp://camargo:60716'}
tcp://camargo:60716 Receive job from worker \00B-\A2 {'function': 'getitem-ack', 'status': 'OK', 'jobid': ('bag-1', 5), 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 526525), 'address': 'tcp://camargo:40401'}
tcp://camargo:40314 Getitem for worker {'function': 'getitem', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 526580), 'jobid': ('bag-1', 8), 'address': 'tcp://camargo:60716'} {'function': 'getitem', 'queue': '4b35789a-0585-11e5-9ab0-34363b801fb8', 'key': ('bag-1', 8)}
tcp://camargo:49982 Receive job from worker \00fZ}\AD {'function': 'getitem', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 526399), 'jobid': ('bag-1', 1), 'address': 'tcp://camargo:60716'}
tcp://camargo:47955 Receive job from worker \00\94\91 \D0 {'function': 'getitem', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 526745), 'jobid': ('bag-1', 3), 'address': 'tcp://camargo:60716'}
tcp://camargo:40314 Send to worker tcp://camargo:60716 {'function': 'getitem-ack', 'status': 'OK', 'address': 'tcp://camargo:40314', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 527856), 'jobid': ('bag-1', 8)}
tcp://camargo:49982 Getitem for worker {'function': 'getitem', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 526399), 'jobid': ('bag-1', 1), 'address': 'tcp://camargo:60716'} {'function': 'getitem', 'queue': '4b35789a-0585-11e5-9ab0-34363b801fb8', 'key': ('bag-1', 1)}
tcp://camargo:47955 Getitem for worker {'function': 'getitem', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 526745), 'jobid': ('bag-1', 3), 'address': 'tcp://camargo:60716'} {'function': 'getitem', 'queue': '4b35789a-0585-11e5-9ab0-34363b801fb8', 'key': ('bag-1', 3)}
tcp://camargo:49982 Send to worker tcp://camargo:60716 {'function': 'getitem-ack', 'status': 'OK', 'address': 'tcp://camargo:49982', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 528087), 'jobid': ('bag-1', 1)}
tcp://camargo:40401 Getitem for worker {'function': 'getitem', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 526304), 'jobid': ('bag-1', 0), 'address': 'tcp://camargo:60716'} {'function': 'getitem', 'queue': '4b35789a-0585-11e5-9ab0-34363b801fb8', 'key': ('bag-1', 0)}
tcp://camargo:40401 Send to worker tcp://camargo:60716 {'function': 'getitem-ack', 'status': 'OK', 'address': 'tcp://camargo:40401', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 528147), 'jobid': ('bag-1', 0)}
tcp://camargo:47955 Send to worker tcp://camargo:60716 {'function': 'getitem-ack', 'status': 'OK', 'address': 'tcp://camargo:47955', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 528248), 'jobid': ('bag-1', 3)}
tcp://camargo:60716 Getitem ack {'queue': '4b35789a-0585-11e5-9ab0-34363b801fb8', 'key': ('bag-1', 5), 'value': 1}
tcp://camargo:60716 Receive job from worker \00B-\A2 {'function': 'getitem-ack', 'status': 'OK', 'jobid': ('bag-1', 4), 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 527164), 'address': 'tcp://camargo:41358'}
tcp://camargo:60716 Getitem ack {'queue': '4b35789a-0585-11e5-9ab0-34363b801fb8', 'key': ('bag-1', 4), 'value': 1}
tcp://camargo:60716 Receive job from worker \00B-\A2 {'function': 'getitem-ack', 'status': 'OK', 'jobid': ('bag-1', 0), 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 528147), 'address': 'tcp://camargo:40401'}
tcp://camargo:60716 Receive job from worker \00B-\A2  {'function': 'getitem-ack', 'status': 'OK', 'jobid': ('bag-1', 8), 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 527856), 'address': 'tcp://camargo:40314'}
tcp://camargo:60716 Getitem ack {'queue': '4b35789a-0585-11e5-9ab0-34363b801fb8', 'key': ('bag-1', 0), 'value': 1}
tcp://camargo:60716 Receive job from worker \00B-\A2! {'function': 'getitem-ack', 'status': 'OK', 'jobid': ('bag-1', 7), 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 527348), 'address': 'tcp://camargo:41341'}
tcp://camargo:60716 Getitem ack {'queue': '4b35789a-0585-11e5-9ab0-34363b801fb8', 'key': ('bag-1', 8), 'value': 1}
tcp://camargo:60716 Getitem ack {'queue': '4b35789a-0585-11e5-9ab0-34363b801fb8', 'key': ('bag-1', 7), 'value': 1}
tcp://camargo:60716 Receive job from worker \00B-\A2" {'function': 'getitem-ack', 'status': 'OK', 'jobid': ('bag-1', 6), 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 526836), 'address': 'tcp://camargo:49982'}
tcp://camargo:60716 Receive job from worker \00B-\A2# {'function': 'getitem-ack', 'status': 'OK', 'jobid': ('bag-1', 3), 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 528248), 'address': 'tcp://camargo:47955'}
tcp://camargo:60716 Getitem ack {'queue': '4b35789a-0585-11e5-9ab0-34363b801fb8', 'key': ('bag-1', 6), 'value': 1}
tcp://camargo:60716 Getitem ack {'queue': '4b35789a-0585-11e5-9ab0-34363b801fb8', 'key': ('bag-1', 3), 'value': 1}
tcp://camargo:60716 Receive job from worker \00B-\A2" {'function': 'getitem-ack', 'status': 'OK', 'jobid': ('bag-1', 1), 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 528087), 'address': 'tcp://camargo:49982'}
tcp://camargo:60716 Getitem ack {'queue': '4b35789a-0585-11e5-9ab0-34363b801fb8', 'key': ('bag-1', 1), 'value': 1}
tcp://camargo:60716 Collect finishes
tcp://camargo:60716 Start computation bag-2 (<built-in function sum>, [('bag-1', 4), ('bag-1', 5), ('bag-1', 6), ('bag-1', 7), ('bag-1', 0), ('bag-1', 1), ('bag-1', 2), ('bag-1', 8), ('bag-1', 3)])
tcp://camargo:60716 End computation bag-2 (<built-in function sum>, [('bag-1', 4), ('bag-1', 5), ('bag-1', 6), ('bag-1', 7), ('bag-1', 0), ('bag-1', 1), ('bag-1', 2), ('bag-1', 8), ('bag-1', 3)]) OK
tcp://camargo:60716 Send to scheduler {'function': 'finished-task'}
tcp://camargo:60716 Receive job from scheduler {'function': 'delitem', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 531439), 'jobid': ('bag-1', 4), 'address': 'tcp://camargo:43659'}
tcp://camargo:60716 Delitem {'key': ('bag-1', 4)}
tcp://camargo:41358 Receive job from scheduler {'function': 'delitem', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 531616), 'jobid': ('bag-1', 4), 'address': 'tcp://camargo:43659'}
tcp://camargo:60716 Receive job from scheduler {'function': 'delitem', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 531801), 'jobid': ('bag-1', 5), 'address': 'tcp://camargo:43659'}
tcp://camargo:41358 Delitem {'key': ('bag-1', 4)}
tcp://camargo:60716 Delitem {'key': ('bag-1', 5)}
tcp://camargo:40401 Receive job from scheduler {'function': 'delitem', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 531992), 'jobid': ('bag-1', 5), 'address': 'tcp://camargo:43659'}
tcp://camargo:40401 Delitem {'key': ('bag-1', 5)}
tcp://camargo:60716 Receive job from scheduler {'function': 'delitem', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 532182), 'jobid': ('bag-1', 6), 'address': 'tcp://camargo:43659'}
tcp://camargo:49982 Receive job from scheduler {'function': 'delitem', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 532422), 'jobid': ('bag-1', 6), 'address': 'tcp://camargo:43659'}
tcp://camargo:60716 Delitem {'key': ('bag-1', 6)}
tcp://camargo:49982 Delitem {'key': ('bag-1', 6)}
tcp://camargo:41341 Receive job from scheduler {'function': 'delitem', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 533104), 'jobid': ('bag-1', 7), 'address': 'tcp://camargo:43659'}
tcp://camargo:60716 Receive job from scheduler {'function': 'delitem', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 532973), 'jobid': ('bag-1', 7), 'address': 'tcp://camargo:43659'}
tcp://camargo:60716 Receive job from scheduler {'function': 'delitem', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 533363), 'jobid': ('bag-1', 0), 'address': 'tcp://camargo:43659'}
tcp://camargo:41341 Delitem {'key': ('bag-1', 7)}
tcp://camargo:40401 Receive job from scheduler {'function': 'delitem', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 533547), 'jobid': ('bag-1', 0), 'address': 'tcp://camargo:43659'}
tcp://camargo:60716 Delitem {'key': ('bag-1', 7)}
tcp://camargo:60716 Delitem {'key': ('bag-1', 0)}
tcp://camargo:40401 Delitem {'key': ('bag-1', 0)}
tcp://camargo:60716 Receive job from scheduler {'function': 'delitem', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 533955), 'jobid': ('bag-1', 1), 'address': 'tcp://camargo:43659'}
tcp://camargo:60716 Delitem {'key': ('bag-1', 1)}
tcp://camargo:49982 Receive job from scheduler {'function': 'delitem', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 534195), 'jobid': ('bag-1', 1), 'address': 'tcp://camargo:43659'}
tcp://camargo:49982 Delitem {'key': ('bag-1', 1)}
tcp://camargo:60716 Receive job from scheduler {'function': 'delitem', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 534415), 'jobid': ('bag-1', 2), 'address': 'tcp://camargo:43659'}
tcp://camargo:60716 Delitem {'key': ('bag-1', 2)}
tcp://camargo:40314 Receive job from scheduler {'function': 'delitem', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 534599), 'jobid': ('bag-1', 8), 'address': 'tcp://camargo:43659'}
tcp://camargo:60716 Receive job from scheduler {'function': 'delitem', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 534773), 'jobid': ('bag-1', 8), 'address': 'tcp://camargo:43659'}
tcp://camargo:40314 Delitem {'key': ('bag-1', 8)}
tcp://camargo:47955 Receive job from scheduler {'function': 'delitem', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 535043), 'jobid': ('bag-1', 3), 'address': 'tcp://camargo:43659'}
tcp://camargo:60716 Delitem {'key': ('bag-1', 8)}
tcp://camargo:47955 Delitem {'key': ('bag-1', 3)}
tcp://camargo:60716 Receive job from scheduler {'function': 'delitem', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 535294), 'jobid': ('bag-1', 3), 'address': 'tcp://camargo:43659'}
tcp://camargo:60716 Delitem {'key': ('bag-1', 3)}
tcp://camargo:60716 Receive job from scheduler {'function': 'getitem', 'timestamp': datetime.datetime(2015, 5, 28, 22, 2, 55, 535813), 'jobid': 'bag-2', 'address': 'tcp://camargo:43659'}
tcp://camargo:60716 Get from scheduler {'queue': '4b370462-0585-11e5-beb0-34363b801fb8', 'key': 'bag-2'}
tcp://camargo:60716 Send to scheduler {'status': 'OK', 'function': 'getitem-ack', 'jobid': 'bag-2'}
```

Traceback on keyboard interrupt of the hang:

``` python
In [10]: x.count().compute()
^C---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-10-35c0464db14a> in <module>()
----> 1 x.count().compute()

/home/blake/github/dask/dask/bag/core.pyc in compute(self, **kwargs)
    166 
    167     def compute(self, **kwargs):
--> 168         return get(self.dask, self.key, **kwargs)
    169 
    170     def apply(self, func):

/home/blake/github/dask/dask/bag/core.pyc in get(dsk, keys, get, **kwargs)
     83     dsk2 = optimize(dsk, keys)
     84 
---> 85     return get(dsk2, keys, **kwargs)
     86 _get = get
     87 

/home/blake/github/dask/dask/distributed/client.pyc in get(self, dsk, keys)
     41 
     42         self.send_to_scheduler(header, payload)
---> 43         header2, payload2 = self.recv_from_scheduler()
     44 
     45         if header2['status'] != 'OK':

/home/blake/github/dask/dask/distributed/client.pyc in recv_from_scheduler(self)
     65 
     66     def recv_from_scheduler(self):
---> 67         header, payload = self.socket.recv_multipart()
     68         header = pickle.loads(header)
     69         loads = header.get('loads', pickle.loads)

/home/blake/.miniconda/envs/dask/lib/python2.7/site-packages/zmq/sugar/socket.pyc in recv_multipart(self, flags, copy, track)
    338 
    339         """
--> 340         parts = [self.recv(flags, copy=copy, track=track)]
    341         # have first part already, only loop while more to receive
    342         while self.getsockopt(zmq.RCVMORE):

zmq/backend/cython/socket.pyx in zmq.backend.cython.socket.Socket.recv (zmq/backend/cython/socket.c:5785)()

zmq/backend/cython/socket.pyx in zmq.backend.cython.socket.Socket.recv (zmq/backend/cython/socket.c:5585)()

zmq/backend/cython/socket.pyx in zmq.backend.cython.socket._recv_copy (zmq/backend/cython/socket.c:1738)()

/home/blake/.miniconda/envs/dask/lib/python2.7/site-packages/zmq/backend/cython/checkrc.pxd in zmq.backend.cython.checkrc._check_rc (zmq/backend/cython/socket.c:6035)()
      9     """
     10     cdef int errno = zmq_errno()
---> 11     PyErr_CheckSignals()
     12     if rc < 0:
     13         if errno == EAGAIN:

KeyboardInterrupt: 

In [11]: 
KeyboardInterrupt
```

Only count?  Do sum/mean work?

After briefly skimming over the logs I don't see a smoking gun.

They do not. 


Resolving patch:
diff --git a/dask/core.py b/dask/core.py
--- a/dask/core.py
+++ b/dask/core.py
@@ -234,12 +234,15 @@ def flatten(seq):
     >>> list(flatten((1, 2, [3, 4]))) # support heterogeneous
     [1, 2, 3, 4]
     """
-    for item in seq:
-        if isinstance(item, list):
-            for item2 in flatten(item):
-                yield item2
-        else:
-            yield item
+    if isinstance(seq, str):
+        yield seq
+    else:
+        for item in seq:
+            if isinstance(item, list):
+                for item2 in flatten(item):
+                    yield item2
+            else:
+                yield item
 
 
 def reverse_dict(d):
diff --git a/dask/distributed/scheduler.py b/dask/distributed/scheduler.py
--- a/dask/distributed/scheduler.py
+++ b/dask/distributed/scheduler.py
@@ -27,9 +27,11 @@
 with open('log.scheduler', 'w') as f:  # delete file
     pass
 
+
 def log(*args):
     with open('log.scheduler', 'a') as f:
-        print(*args, file=f)
+        print('\n', *args, file=f)
+
 
 @contextmanager
 def logerrors():
diff --git a/dask/distributed/worker.py b/dask/distributed/worker.py
--- a/dask/distributed/worker.py
+++ b/dask/distributed/worker.py
@@ -1,24 +1,26 @@
 from __future__ import print_function
 
+import uuid
+import random
 import socket
+import sys
+import traceback
 from threading import Thread, Lock
 from multiprocessing.pool import ThreadPool
 from contextlib import contextmanager
-import traceback
 from datetime import datetime
-import uuid
-import random
-import multiprocessing
-import zmq
 from time import time
-import sys
-from ..compatibility import Queue, unicode
-from .. import core
+
 try:
     import cPickle as pickle
 except ImportError:
     import pickle
-import dill
+
+import zmq
+
+from ..compatibility import Queue, unicode
+from .. import core
+
 
 def pickle_dumps(obj):
     return pickle.dumps(obj, protocol=pickle.HIGHEST_PROTOCOL)
@@ -28,9 +30,11 @@ def pickle_dumps(obj):
 with open('log.workers', 'w') as f:  # delete file
     pass
 
+
 def log(*args):
     with open('log.workers', 'a') as f:
-        print(*args, file=f)
+        print('\n', *args, file=f)
+
 
 log('Hello from worker.py')
 


Test patch:
diff --git a/dask/distributed/tests/test_ipython_utils.py b/dask/distributed/tests/test_distributed.py
similarity index 87%
rename from dask/distributed/tests/test_ipython_utils.py
rename to dask/distributed/tests/test_distributed.py
--- a/dask/distributed/tests/test_ipython_utils.py
+++ b/dask/distributed/tests/test_distributed.py
@@ -1,3 +1,5 @@
+"""Tests that require a running dask.distributed cluster"""
+
 import time
 from subprocess import Popen, PIPE
 
@@ -13,6 +15,7 @@
 
 from dask.distributed import dask_client_from_ipclient
 import dask.array as da
+import dask.bag as db
 
 
 def setup_module():
@@ -68,3 +71,10 @@ def test_dask_client_from_ipclient():
     finally:
         # close the workers
         dask_client.close(close_scheduler=True)
+
+
+def test_gh_248():
+    """bag.sum() and bag.count() would hang with the distributed scheduler"""
+    a = db.from_sequence(range(50), npartitions=2)
+    a.sum().compute()
+    a.count().compute()
diff --git a/dask/tests/test_core.py b/dask/tests/test_core.py
--- a/dask/tests/test_core.py
+++ b/dask/tests/test_core.py
@@ -130,6 +130,7 @@ def test_get_stack_limit():
 
 def test_flatten():
     assert list(flatten(())) == []
+    assert list(flatten('foo')) == ['foo']
 
 
 def test_subs():