Broadcast from multitasked routine ? #2171
-
|
Getting "this resource cannot be used in two tasks at once." but I am only calling hub.ble.broadcast() in one place in one coroutine ? Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 8 replies
-
|
Are you missing https://docs.pybricks.com/en/latest/hubs/primehub.html#pybricks.hubs.PrimeHub.ble.broadcast |
Beta Was this translation helpful? Give feedback.
-
|
As opposed to , for a non-await function call ? How is that different to normal blocking?
… On 28 Apr 2025, at 21:53, David Lechner ***@***.***> wrote:
await suspends the coroutine until the operation finishes. The broadcast function has to write the info to the Bluetooth chip, which takes some amount of time to complete.
—
Reply to this email directly, view it on GitHub <#2171 (reply in thread)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/AKHCFEY6C652HNZINRYLIZD232BNPAVCNFSM6AAAAAB4A4YZAWVHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTEOJXGM3TKMA>.
You are receiving this because you authored the thread.
|
Beta Was this translation helpful? Give feedback.
-
|
So Await basically sends the processor back to the main thread but with a note to return to the coroutine task once the function call completes?
Thanks / I read that thread programming is even harder ..
P
… On 28 Apr 2025, at 22:32, David Lechner ***@***.***> wrote:
Anything that has to communication over an I/O bus (e.g. UART, SPI, I2C) takes significantly longer than executing CPU instructions. So we consider these "blocking" functions and need to be awaited. This way, they don't prevent other coroutines from running.
Similarly, if you have long running or infinite loops, they need at least one await inside the loop to yield to other coroutines (await wait(0) is enough).
—
Reply to this email directly, view it on GitHub <#2171 (reply in thread)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/AKHCFE7AONTQ2ABN7T5GBQL232F6XAVCNFSM6AAAAAB4A4YZAWVHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTEOJXGQYDCMA>.
You are receiving this because you authored the thread.
|
Beta Was this translation helpful? Give feedback.
-
|
Re: Broadcast OSError |
Beta Was this translation helpful? Give feedback.
remote.light.onandhub.ble.broadcastboth use Bluetooth. When both are called at the same time, you'll seethis resource cannot be used in two tasks at once.You could resolve it as follows:
hub.ble.broadcastdirectly, instead set a variable such asbroadcast_datato your desired value.remote.light.ondirectly, instead set a variable such asremote_colorto your desired value.broadcast_datadoes not equalold_broadcast_data(depending on your data, perhaps check equality by element)broadcast(broadcast_data)old_broadcast_dataequal tobroadcast_dataremote_colordoes not equalo…