Buffer Interprocess Sharing: Examples - Python
Buffer Interprocess Sharing | |
---|---|
BIPS Basics | |
|
|
Getting Started | |
|
|
User Manual | |
|
|
Examples | |
|
|
Performance | |
|
|
Contact Us | |
|
Introduction
Same as the C++ examples, Python examples take into account a possible case:
- One-to-one communication
The example binaries are compiled after building. If the build directory is build
:
├── python └── examples └── one-to-one ├── consumer.py └── producer.py
Executable options
Both Python scripts have the same CLI options. Please, take them into account:
usage: bips_client [-h] [--backend BACKEND] [--channel CHANNEL] [--num-buffers NUM_BUFFERS] [--buffer-size BUFFER_SIZE] [--exec-order EXEC_ORDER] [--timeout TIMEOUT] [--num-iterations NUM_ITERATIONS] [--logfile LOGFILE] Example client for BIPS optional arguments: -h, --help show this help message and exit --backend BACKEND Use the specified inter-process shared memory backend; BACKEND can be 'shm' or 'nvsci' --channel CHANNEL Connect to the specified shared memory channel. CHANNEL preferably starts with a / character followed by up to 255 characters, none of which are slashes --num-buffers NUM_BUFFERS Indicate how many shared memory buffers to create; NUM_BUFFERS can be a positive integer --buffer-size BUFFER_SIZE Indicate the size in bytes of a shared memory buffer; BUFFER_SIZE can be a positive integer --exec-order EXEC_ORDER Follow execution order for buffer processing specified in ORDER; ORDER can be 'in-order' or 'out-of-order' --timeout TIMEOUT Indicate how long to wait for available buffers before throwing a timeout error; TIMEOUT is measured in seconds and can be a positive integer --num-iterations NUM_ITERATIONS NUM-ITERATIONS: Indicate how long many buffer processing iterations to run before exiting; NUM-ITERATIONS can be a positive integer --logfile LOGFILE Indicate name of file to log to
Simple examples
These examples show how easy BIPS is to use and they do not have any argument. However, one can modify these examples to check other options.
For instance, consider the following producer:
import numpy as np import bips ## For building a producer backend = bips.Backends.kShm channel = "/example_channel" buffers = 4 size = 3840 * 2160 * 4 in_order = True producer = bips.Producer(backend,channel,buffers,size,in_order) num_iterations = 120 timeout = 6000000 max_modified_values = 120 for i in range(num_iterations): buffer = producer.Pull(timeout) np_array = np.array(buffer.data, copy=False) for j in range(max_modified_values): np_array[j] = 32 np_array[0] = i producer.Push(buffer, timeout)
and a consumer:
import numpy as np import bips ## For building a consumer backend = bips.Backends.kShm channel = "/example_channel" buffers = 4 size = 3840 * 2160 * 4 in_order = True consumer = bips.Consumer(backend,channel,buffers,size,in_order) num_iterations = 120 timeout = 6000000 for i in range(num_iterations): buffer = consumer.Pull(timeout) np_array = np.array(buffer.data, copy=False) print(np_array[0]) consumer.Push(buffer, timeout)
The options from the producer are:
backend
: in this case, only kShm is supportedchannel
: channel name. BIPS only supports a channel per producerbuffers
: number of buffers of the buffer poolsize
: size of each bufferin_order
: defines an in-order execution (FIFO-like)
The options from the producer must be the same in the associated consumers.
Constructors
bips.Producer(backend, channel, buffers, size, in_order)
: producerbips.Consumer(backend, channel, buffers, size, in_order)
: consumer
Usage
buffer = bips_object.Pull(timeout)
: pulls a buffer to write on. It is compatible with NumPy. The timeout is given in microseconds.bips_object.Push(buffer, timeout)
: pushes a buffer to the buffer pool (producer makes available a buffer for reading, consumer frees the buffer for new writings). The timeout is given in microseconds.
Note: The buffer must be written in-place in the case of producers. The same buffer must be returned in the consumer when pushing.
Running the examples
One-to-one example
1. Open two different terminals: one for the producer and another for the consumer.
2. In terminal 1, execute the producer:
python3 producer.py
3. In terminal 2, execute the consumer:
python3 consumer.py