示例#1
0
 def test_nowait_read_writes(self):
     LAYOUT = "int8_2"
     LENGTH = 500
     loop = asyncio.get_event_loop()
     my_pipe = LocalPipe(LAYOUT)
     my_subscriber = LocalPipe(LAYOUT)
     my_pipe.subscribe(my_subscriber)
     test_data = helpers.create_data(LAYOUT, length=LENGTH)
     my_pipe.write_nowait(test_data)
     result = my_pipe.read_nowait()
     # read data should match written data
     np.testing.assert_array_almost_equal(test_data['data'], result['data'])
     np.testing.assert_array_almost_equal(test_data['timestamp'],
                                          result['timestamp'])
     # subscriber should have a copy too
     result = my_subscriber.read_nowait()
     np.testing.assert_array_almost_equal(test_data['data'], result['data'])
     np.testing.assert_array_almost_equal(test_data['timestamp'],
                                          result['timestamp'])
示例#2
0
    def test_nowait_function_exceptions(self):
        pipe = LocalPipe(layout="float32_3")
        subscriber = LocalPipe(layout="float32_3", close_cb=mock.Mock())
        fd_subscriber = OutputPipe()
        # cannot close_nowait if there is a callback
        with self.assertRaises(PipeError):
            subscriber.close_nowait()

        # can write_nowait if only localpipe subscribers
        pipe.subscribe(subscriber)
        pipe.write_nowait(np.ones((100, 4)))
        # cannot if any subscribers are not localpipes
        pipe.subscribe(fd_subscriber)
        with self.assertRaises(PipeError):
            pipe.write_nowait(np.ones((100, 4)))

        # cannot close_nowait if there are subscribers
        pipe.subscribe(subscriber)
        with self.assertRaises(PipeError):
            pipe.close_nowait()
示例#3
0
    def test_pipes_numpy_arrays(self):
        """writes to pipe sends data to reader and any subscribers"""
        LAYOUT = "int8_2"
        LENGTH = 1003
        loop = asyncio.get_event_loop()
        my_pipe = LocalPipe(LAYOUT, name="pipe")
        # test timeouts, since the writer is slow
        my_pipe.TIMEOUT_INTERVAL = 0.05
        subscriber_pipe = LocalPipe(LAYOUT)
        subscriber_pipe2 = LocalPipe(LAYOUT)
        my_pipe.subscribe(subscriber_pipe)
        my_pipe.subscribe(subscriber_pipe2)
        test_data = helpers.create_data(LAYOUT, length=LENGTH)
        reader_rx_data = np.empty(test_data.shape, test_data.dtype)
        subscriber_rx_data = np.empty(test_data.shape, test_data.dtype)

        #    print(test_data['data'][:,1])
        async def writer():
            for block in helpers.to_chunks(test_data, 270):
                await asyncio.sleep(0.1)
                await my_pipe.write(block)
            await my_pipe.close()

        async def reader():
            # note this is kind of delicate, you can only read data twice
            # if the pipe is closed so it might look like you didn't finish all
            # the data if the last read ignores data past blk_size
            blk_size = 357
            rx_idx = 0
            while not my_pipe.is_empty():
                # consume data in pipe
                data_chunk = await my_pipe.read()
                rows_used = min(len(data_chunk), blk_size)
                reader_rx_data[rx_idx:rx_idx +
                               rows_used] = data_chunk[:rows_used]
                rx_idx += rows_used
                my_pipe.consume(rows_used)

        async def subscriber():
            rx_idx = 0
            while not subscriber_pipe.is_empty():
                # consume data in pipe
                data_chunk = await subscriber_pipe.read()
                subscriber_rx_data[rx_idx:rx_idx +
                                   len(data_chunk)] = data_chunk
                rx_idx += len(data_chunk)
                subscriber_pipe.consume(len(data_chunk))

        loop = asyncio.get_event_loop()
        tasks = [
            asyncio.ensure_future(writer()),
            asyncio.ensure_future(reader()),
            asyncio.ensure_future(subscriber())
        ]

        loop.run_until_complete(asyncio.gather(*tasks))

        data = subscriber_pipe2.read_nowait()
        np.testing.assert_array_equal(test_data, data)
        np.testing.assert_array_equal(test_data, reader_rx_data)
        np.testing.assert_array_equal(test_data, subscriber_rx_data)