How to use the strax.Mailbox function in strax

To help you get started, we’ve selected a few strax examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github AxFoundation / strax / tests / test_mailbox.py View on Github external
def mailbox_tester(messages,
                   numbers=None,
                   reader_sleeps=0.,
                   max_messages=100,
                   expected_result=None,
                   timeout=SHORT_TIMEOUT,
                   result_timeout=LONG_TIMEOUT):
    if numbers is None:
        numbers = np.arange(len(messages))
    if expected_result is None:
        messages = np.asarray(messages)
        expected_result = messages[np.argsort(numbers)]

    mb = strax.Mailbox(max_messages=max_messages, timeout=timeout)

    n_readers = 2

    with concurrent.futures.ThreadPoolExecutor() as tp:
        futures = [tp.submit(reader,
                             source=mb.subscribe(),
                             reader_sleeps=reader_sleeps)
                   for _ in range(n_readers)]

        for i in range(len(messages)):
            mb.send(messages[i], msg_number=numbers[i])
            print(f"Sent message {i}. Now {len(mb._mailbox)} ms in mailbox.")

        mb.close()

        # Results must be equal
github AxFoundation / strax / tests / test_mailbox.py View on Github external
def test_deadlock_regression():
    """A reader thread may start after the first message is processed"""
    mb = strax.Mailbox(timeout=SHORT_TIMEOUT)
    mb.send(0)

    readers = [
        threading.Thread(target=reader,
                         kwargs=dict(
                             source=mb.subscribe(),
                             name=str(i)))
        for i in range(2)
    ]
    readers[0].start()
    time.sleep(SHORT_TIMEOUT)

    readers[1].start()
    mb.send(1)
    mb.close()
github AxFoundation / strax / tests / test_mailbox.py View on Github external
def test_highlevel():
    """Test highlevel mailbox API"""
    mb = strax.Mailbox()
    mb.add_sender(range(10))

    def test_reader(source):
        test_reader.got = r = []
        for s in source:
            r.append(s)

    mb.add_reader(test_reader)
    mb.start()
    time.sleep(SHORT_TIMEOUT)
    assert hasattr(test_reader, 'got')
    assert test_reader.got == list(range(10))
    mb.cleanup()
    assert len(threading.enumerate()) == 1, "Not all threads died"
github AxFoundation / strax / tests / test_parallelsourceplugin.py View on Github external
def test_processing():
    """Test ParallelSource plugin under several conditions"""
    # It's always harder with a small mailbox:
    strax.Mailbox.DEFAULT_MAX_MESSAGES = 2
    for request_peaks in (True, False):
        for peaks_parallel in (True, False):
            for max_workers in (1, 2):
                Peaks.parallel = peaks_parallel
                print(f"\nTesting with request_peaks {request_peaks}, "
                      f"peaks_parallel {peaks_parallel}, "
                      f"max_workers {max_workers}")

                mystrax = strax.Context(storage=[],
                                        register=[Records, Peaks])
                bla = mystrax.get_array(
                    run_id=run_id,
                    targets='peaks' if request_peaks else 'records',
                    max_workers=max_workers)
                assert len(bla) == recs_per_chunk * n_chunks
                assert bla.dtype == (
github AxFoundation / strax / tests / test_mailbox.py View on Github external
def test_valid_msg_number():
    """Message numbers are non-negative integers"""
    mb = strax.Mailbox()
    with pytest.raises(strax.InvalidMessageNumber):
        mb.send(0, msg_number=-1)
    with pytest.raises(strax.InvalidMessageNumber):
        mb.send(0, msg_number='???')
github AxFoundation / strax / strax / processor.py View on Github external
def __missing__(self, key):
        res = self[key] = strax.Mailbox(name=key + '_mailbox',
                                        lazy=self.lazy)
        return res