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 Multiple Simultaneous Connections in Python SDK #124

Open
PipGrylls opened this issue Feb 2, 2022 · 5 comments
Open

Support Multiple Simultaneous Connections in Python SDK #124

PipGrylls opened this issue Feb 2, 2022 · 5 comments
Labels
enhancement New feature or request help wanted Extra attention is needed python sdk Python SDK

Comments

@PipGrylls
Copy link

PipGrylls commented Feb 2, 2022

This ticket is being used to track development / testing of simultaneous connections. The original bug request is shown below.

===================================================================

Connecting to cameras simultaneously seems to cause issues when disconnecting.

Hardware:
2x GoProHero10, Raspberry Pi 4b 2Gb

I am using an ExitStack to control the contexts but I can recreate the issue without using a context manager.
Note: the wifi is not enabled as the Pi is controlled on the network over wifi and it will be disconnected if wifi is enabled.

I think there is an issue in releasing the async disconnect event. One camera will connect and disconnect fine.

Tomorrow I will edit the GoPro class to try to release whatever event is blocking and will update the issue/create a PR if I find anything.

Minimal reproduction:

from contextlib import ExitStack

from open_gopro import GoPro


camera_list = [
    {
        'target': "GoPro ****",
        'enable_wifi': False,
    },
    {
        'target': "GoPro ****",
        'enable_wifi': False,
    },
]

stack = ExitStack()
cameras = [
    stack.enter_context(
        GoPro(
            target=camera_args.get('target'),
            enable_wifi=camera_args.get('enable_wifi', False)
        )
    )
    for camera_args in camera_list
]

for camera in cameras:
    print(camera.is_ble_connected)

stack.pop_all().close()

And here is the traceback:

Traceback (most recent call last):
  File "/home/pi/GoPro/minimal_test.py", line 31, in <module>
    stack.pop_all().close()
  File "/usr/lib/python3.9/contextlib.py", line 521, in close
    self.__exit__(None, None, None)
  File "/usr/lib/python3.9/contextlib.py", line 513, in __exit__
    raise exc_details[1]
  File "/usr/lib/python3.9/contextlib.py", line 498, in __exit__
    if cb(*exc_details):
  File "/home/pi/.local/lib/python3.9/site-packages/open_gopro/gopro.py", line 185, in __exit__
    self.close()
  File "/home/pi/.local/lib/python3.9/site-packages/open_gopro/gopro.py", line 350, in close
    self._close_ble()
  File "/home/pi/.local/lib/python3.9/site-packages/open_gopro/gopro.py", line 554, in _close_ble
    self._ble.close()
  File "/home/pi/.local/lib/python3.9/site-packages/open_gopro/ble/client.py", line 108, in close
    self._controller.disconnect(self._handle)
  File "/home/pi/.local/lib/python3.9/site-packages/open_gopro/ble/adapters/bleak_wrapper.py", line 299, in disconnect
    return self._as_coroutine(_async_disconnect)
  File "/home/pi/.local/lib/python3.9/site-packages/open_gopro/ble/adapters/bleak_wrapper.py", line 67, in _as_coroutine
    return asyncio.run_coroutine_threadsafe(action(), self._module_loop).result(timeout)
  File "/usr/lib/python3.9/concurrent/futures/_base.py", line 440, in result
    return self.__get_result()
  File "/usr/lib/python3.9/concurrent/futures/_base.py", line 389, in __get_result
    raise self._exception
  File "/home/pi/.local/lib/python3.9/site-packages/open_gopro/ble/adapters/bleak_wrapper.py", line 295, in _async_disconnect
    await handle.disconnect()
  File "/home/pi/.local/lib/python3.9/site-packages/bleak/backends/bluezdbus/client.py", line 459, in disconnect
    reply = await self._bus.call(
  File "/home/pi/.local/lib/python3.9/site-packages/dbus_next/aio/message_bus.py", line 305, in call
    await future
RuntimeError: Task <Task pending name='Task-30' coro=<BleakWrapperController.disconnect.<locals>._async_disconnect() running at /home/pi/.local/lib/python3.9/site-packages/open_gopro/ble/adapters/bleak_wrapper.py:295> cb=[_chain_future.<locals>._call_set_state() at /usr/lib/python3.9/asyncio/futures.py:391]> got Future <Future pending> attached to a different loop
@PipGrylls PipGrylls added the bug Something isn't working label Feb 2, 2022
@PipGrylls PipGrylls changed the title Issue when controlling multiple cameras Issue disconnecting BLE when controlling multiple cameras Feb 3, 2022
@PipGrylls
Copy link
Author

Update: PipGrylls#1 (comment)

@PipGrylls
Copy link
Author

Using the following code I can connect and disconnect from two cameras without hitting the issue. This leads me to follow the logic that the event loop needs to be able to have cameras added to it or threads need to specifically carry their own cameras to work in parallel? See simple connect disconnect on a single event loop here https://gist.github.com/PipGrylls/79b3cc09a5f90e9fd3acba8cfa803100

@tcamise-gpsw tcamise-gpsw added the help wanted Extra attention is needed label Feb 7, 2022
@tcamise-gpsw
Copy link
Collaborator

Hello. While the SDK has been designed to allow control of multiple cameras simultaneously, this is currently largely untested. I do plan on getting around to this at some point but I can't say when that will be at this time.

@tcamise-gpsw tcamise-gpsw added enhancement New feature or request and removed bug Something isn't working labels Aug 24, 2022
@tcamise-gpsw tcamise-gpsw changed the title Issue disconnecting BLE when controlling multiple cameras Support Multiple Simultaneous Connections in Python SDK Sep 15, 2022
@PengKunPROO
Copy link

Hi! I found the same problem, have you fixed up this issue?

@tcamise-gpsw
Copy link
Collaborator

I have not done any testing on this. It definitely needs some work.

@tcamise-gpsw tcamise-gpsw added demos Relating to demos (not SDKs) and removed python_sdk labels Jan 4, 2023
@tcamise-gpsw tcamise-gpsw pinned this issue Jan 7, 2025
@tcamise-gpsw tcamise-gpsw added python sdk Python SDK and removed demos Relating to demos (not SDKs) labels Jan 7, 2025
@github-project-automation github-project-automation bot moved this to To do in Python SDK Jan 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed python sdk Python SDK
Projects
Status: To do
Development

No branches or pull requests

3 participants