def test_thread_locality(self): store = localstore.LocalStore() store['name'] = "Hi" q = Queue() def do_test(): sleep(0.1) t = current_thread() name = t.name store[name] = 1 store['name'] = name q.put(dict(store)) threads = [] for x in range(5): t = Thread(target=do_test, name='thread%x' % x) t.start() threads.append(t) for t in threads: t.join() # assert none of the thread stuff touched our queue self.assertEqual(store['name'], 'Hi') self.assertEqual(q.qsize(), 5) qcontents = [] while not q.empty(): qcontents.append(q.get()) self.assertEqual(len(qcontents), 5) for d in qcontents: self.assertEqual(len(d), 2) self.assertNotEqual(d['name'], 'Hi') self.assertEqual(d[d['name']], 1)
class message_queue(object): """Return a message queue that gets 'hit' or 'miss' messages. The signal handlers use weakrefs, so if we don't save references to this object they will get gc'd pretty fast.""" def __init__(self): self.q = Queue() qc_hit.connect(self._hit) qc_miss.connect(self._miss) qc_skip.connect(self._skip) def _hit(self, *a, **k): self.q.put(True) def _miss(self, *a, **k): self.q.put(False) def _skip(self, *a, **k): self.q.put(False) def clear(self): while not self.q.empty(): self.q.get_nowait() def get(self): return self.q.get() def get_nowait(self): return self.q.get_nowait() def qsize(self): return self.q.qsize() def empty(self): return self.q.empty()