# Async non blocking API calls

**URL:** <https://forum.legato.io/t/async-non-blocking-api-calls/6649>\
**Category:** Uncategorized\
**Created:** [November 28, 2023, 2:18pm UTC](https://forum.legato.io/t/async-non-blocking-api-calls/6649 "2023-11-28T14:18:04Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![gi1](https://avatars.discourse-cdn.com/v4/letter/g/e79b87/32.png) [@gi1](https://forum.legato.io/u/gi1)\
**Post date:** [November 28, 2023, 2:18pm UTC](https://forum.legato.io/t/async-non-blocking-api-calls/6649/1 "2023-11-28T14:18:04Z")

</div>

Hello,  
I’m in this case:

Component A (use B api function)  
Component B (exposes api functions)

Component B handle “Data Connection”, it open GPRS context and activate data. I use built in ` **le\_data\_ConnectService()** and then **le\_data\_Request()** for this. Naturally if connection is down, Component B can take time to ativate.

Componet A, instead, watch for some events (motion) and if needed it call Componet B for connection.

My question is, **how to avoid blocking A while calling B apis during works?**

Sorry for my questin but i cant find a clear documenation on this. I’ve tried calling B methods from A using thread but got a problem here… cause the le\_data\_ConnectService(); needs to be called from the main thread. So i Receive an error.

Thank you so much,  
Gian

---

<div class="post-metadata">

**Author:** ![jyijyi](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.legato.io/jyijyi/32/822_2.png) [@jyijyi](https://forum.legato.io/u/jyijyi)\
**Post date:** [November 28, 2023, 2:30pm UTC](https://forum.legato.io/t/async-non-blocking-api-calls/6649/2 "2023-11-28T14:30:50Z")

</div>

Then can you using other higher priority thread to handle motion event in component A?

---

<div class="post-metadata">

**Author:** ![gi1](https://avatars.discourse-cdn.com/v4/letter/g/e79b87/32.png) [@gi1](https://forum.legato.io/u/gi1)\
**Post date:** [November 28, 2023, 2:31pm UTC](https://forum.legato.io/t/async-non-blocking-api-calls/6649/3 "2023-11-28T14:31:52Z")

</div>

Thanks for your answer…

Can you be more clear?

---

<div class="post-metadata">

**Author:** ![jyijyi](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.legato.io/jyijyi/32/822_2.png) [@jyijyi](https://forum.legato.io/u/jyijyi)\
**Post date:** [November 28, 2023, 2:33pm UTC](https://forum.legato.io/t/async-non-blocking-api-calls/6649/4 "2023-11-28T14:33:46Z")

</div>

You say that “le\_data\_ConnectService(); needs to be called from the main thread”

Then you can create other thread to handle motion event, right?

---

<div class="post-metadata">

**Author:** ![gi1](https://avatars.discourse-cdn.com/v4/letter/g/e79b87/32.png) [@gi1](https://forum.legato.io/u/gi1)\
**Post date:** [November 28, 2023, 2:36pm UTC](https://forum.legato.io/t/async-non-blocking-api-calls/6649/5 "2023-11-28T14:36:25Z")

</div>

Yes, from component A i can create a Thread to alert Component B that “data connection” is needed, and i can set this to high priority. But anyway … when calling B api… (Connect)… in this function i call le\_data\_ConnectService(), and it goves me an error: **le\_data\_ConnectService** must be called from main thread…

---

<div class="post-metadata">

**Author:** ![gi1](https://avatars.discourse-cdn.com/v4/letter/g/e79b87/32.png) [@gi1](https://forum.legato.io/u/gi1)\
**Post date:** [November 28, 2023, 2:38pm UTC](https://forum.legato.io/t/async-non-blocking-api-calls/6649/6 "2023-11-28T14:38:00Z")

</div>

The only way i found is using a Timer in B to check is something changes… and start actions based on some variable changed. I’m using this approach in another component… but this fill the code of timers…

---

<div class="post-metadata">

**Author:** ![jyijyi](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.legato.io/jyijyi/32/822_2.png) [@jyijyi](https://forum.legato.io/u/jyijyi)\
**Post date:** [November 28, 2023, 2:44pm UTC](https://forum.legato.io/t/async-non-blocking-api-calls/6649/7 "2023-11-28T14:44:55Z")

</div>

Then why don’t you call le\_data\_ConnectService() in the main thread?

---

<div class="post-metadata">

**Author:** ![gi1](https://avatars.discourse-cdn.com/v4/letter/g/e79b87/32.png) [@gi1](https://forum.legato.io/u/gi1)\
**Post date:** [November 28, 2023, 2:49pm UTC](https://forum.legato.io/t/async-non-blocking-api-calls/6649/8 "2023-11-28T14:49:05Z")

</div>

if calling in main thread means calling a direct function from extern, via a method… then this will block until connection established. And I dont want this, cause this will block other part of code that must flow normally without wait…

☹

---

<div class="post-metadata">

**Author:** ![jyijyi](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.legato.io/jyijyi/32/822_2.png) [@jyijyi](https://forum.legato.io/u/jyijyi)\
**Post date:** [November 28, 2023, 2:50pm UTC](https://forum.legato.io/t/async-non-blocking-api-calls/6649/9 "2023-11-28T14:50:40Z")

</div>

Other part of code should be run in another higher priority thread, not same main thread

---

<div class="post-metadata">

**Author:** ![gi1](https://avatars.discourse-cdn.com/v4/letter/g/e79b87/32.png) [@gi1](https://forum.legato.io/u/gi1)\
**Post date:** [November 28, 2023, 2:52pm UTC](https://forum.legato.io/t/async-non-blocking-api-calls/6649/10 "2023-11-28T14:52:52Z")

</div>

will try to make some changes following your direction…  
thank so much … you really kind.

Gian
