Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support a callback method that is called every time a new buffer arrived. #61

Closed
kazunarikudo opened this issue Nov 9, 2018 · 1 comment
Labels
improvement New feature or improvement
Milestone

Comments

@kazunarikudo
Copy link
Member

kazunarikudo commented Nov 9, 2018

Is your feature request related to a problem? Please describe.
As of version 0.2.2, ImageAcquirer provides limited ways of checking filled buffers those are being internally held. One is calling fetch_buffer or another is probing the num_holding_filled_buffers property. Both ways are necessary and useful for most cases but they are not suitable if the application concerns the cost of polling.

Describe the solution you'd like
Support a callback method that is called every time when a new buffer arrived. Having this method, the application is released from the concern on polling. The method should be able to get registered through a property and it would be nice if we could name the property on_new_buffer_arrival.

Describe alternatives you've considered
None.

Additional context
Due to the nature of the callback method that is called in the image acquisition thread worker, you should minimize the duration you spend in the callback function as much as possible. If it takes too long, then the thread could drop images those should have been acquired if you didn't take such a long time.

In addition, note that the callback is just telling you that a new buffer has arrived. It doesn't mean the ImageAcqurier object is holding just one buffer at the moment or the new buffer has still been held. Depending on the configuration of the ImageAcquirer object, it may be holding one or more buffers or the new buffer might have been already discarded. If you want to know the number of buffers that it's holding, just refer the num_holding_filled_buffers property.

@kazunarikudo kazunarikudo added the improvement New feature or improvement label Nov 9, 2018
@kazunarikudo kazunarikudo added this to the 0.2.3 milestone Nov 9, 2018
@kazunarikudo
Copy link
Member Author

The on_new_buffer_arrival property can be used as follows:

def test_issue_61(self):
    if not self.is_running_with_default_target():
        return

    # Connect to the first camera in the list.
    self.ia = self.harvester.create_image_acquirer(0)

    # Register a call back method:
    self.ia.on_new_buffer_arrival = self._callback_on_new_buffer_arrival

    # We turn software tirrger on:
    self.setup_camera()

    # We have not fetched any buffer:
    self.assertEqual(0, len(self._buffers))

    # Start image acquisition:
    self.ia.start_image_acquisition()

    # Trigger the target device:
    num_images = self.ia.num_buffers
    self.assertTrue(num_images > 0)

    # Trigger the target device:
    for _ in range(num_images):
        self.generate_software_trigger()
        # Note that we should have another reliable way to confirm
        # FRAME_TRIGGER_WAIT.
        time.sleep(0.01)

    # If the callback method was called, then we should have the same
    # number of buffers with num_images:
    self.assertEqual(num_images, len(self._buffers))

    # Release the buffers before stopping image acquisition:
    for buffer in self._buffers:
        buffer.queue()

    self._buffers.clear()

    # Then stop image acquisition:
    self.ia.stop_image_acquisition()

def _callback_on_new_buffer_arrival(self):
    # Fetch a buffer and keep it:
    self._buffers.append(self.ia.fetch_buffer())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
improvement New feature or improvement
Projects
None yet
Development

No branches or pull requests

1 participant