# Type of calls allowed inside Erlang Process (sync/async, blocking/nonblocking)

**URL:** <https://erlangforums.com/t/type-of-calls-allowed-inside-erlang-process-sync-async-blocking-nonblocking/1951>\
**Category:** Questions / Help\
**Tags:** learning-erlang, erlang-otp\
**Created:** [October 17, 2022, 2:18pm UTC](https://erlangforums.com/t/type-of-calls-allowed-inside-erlang-process-sync-async-blocking-nonblocking/1951 "2022-10-17T14:18:17Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![mohan](https://erlangforums.com/user_avatar/erlangforums.com/mohan/32/1657_2.png) [@mohan](https://erlangforums.com/u/mohan)\
**Post date:** [October 17, 2022, 2:18pm UTC](https://erlangforums.com/t/type-of-calls-allowed-inside-erlang-process-sync-async-blocking-nonblocking/1951/1 "2022-10-17T14:18:17Z")

</div>

I have the following queries on the type of calls supported in Erlang. Please clarify.

- Akka advices to avoid Blocking calls in the actors. Is there any similar advice/restriction in Erlang?
- Java supports Async APIs to overcome the limitation set on the number of threads per JVM. Since users are allowed to create many Erlang Processes, I understand that Async APIs are NOT needed in Erlang. Please confirm.
- If there is any material on Erlang best practices, patterns or anti-pattern, please point me to them.

Reference:

- [Akka best practice](https://doc.akka.io/docs/akka/current/general/actor-systems.html#actor-best-practices)

---

<div class="post-metadata">

**Author:** ![max-au](https://erlangforums.com/user_avatar/erlangforums.com/max-au/32/1627_2.png) [@max-au](https://erlangforums.com/u/max-au)\
**Post date:** [October 17, 2022, 10:59pm UTC](https://erlangforums.com/t/type-of-calls-allowed-inside-erlang-process-sync-async-blocking-nonblocking/1951/2 "2022-10-17T22:59:41Z")

</div>

There is no such thing as “call” from one process to another in Erlang. Unless you’re talking of `gen_server:call` flow, which is a sequence of monitor + sending a message + waiting for reply/timeout/monitor.

You can make any number of “asynchronous calls” (often referred as “multicall”) by sending “request” messages and collecting replies.

---

<div class="post-metadata">

**Author:** ![lpil](https://erlangforums.com/user_avatar/erlangforums.com/lpil/32/222_2.png) [@lpil](https://erlangforums.com/u/lpil)\
**Post date:** [October 18, 2022, 9:20am UTC](https://erlangforums.com/t/type-of-calls-allowed-inside-erlang-process-sync-async-blocking-nonblocking/1951/3 "2022-10-18T09:20:21Z")

</div>

> [@mohan](#):
>
> Akka advices to avoid Blocking calls in the actors. Is there any similar advice/restriction in Erlang?

There’s no such restriction, there are no functions in Erlang which will block the OS thread. Call any function you like!

> [@mohan](#):
>
> Java supports Async APIs to overcome the limitation set on the number of threads per JVM. Since users are allowed to create many Erlang Processes, I understand that Async APIs are NOT needed in Erlang. Please confirm.

That’s correct, you can create millions of processes and don’t need to use specific functions in them.

---

<div class="post-metadata">

**Author:** ![mohan](https://erlangforums.com/user_avatar/erlangforums.com/mohan/32/1657_2.png) [@mohan](https://erlangforums.com/u/mohan)\
**Post date:** [October 18, 2022, 11:44am UTC](https://erlangforums.com/t/type-of-calls-allowed-inside-erlang-process-sync-async-blocking-nonblocking/1951/4 "2022-10-18T11:44:29Z")

</div>

Thanks Max. Yes, the Erlang processes communicate through messages. I am referring to calls made by a Process to perform IO or network operations (Socket, ssh, sftp, Netconf, etc)

---

<div class="post-metadata">

**Author:** ![mohan](https://erlangforums.com/user_avatar/erlangforums.com/mohan/32/1657_2.png) [@mohan](https://erlangforums.com/u/mohan)\
**Post date:** [October 18, 2022, 11:47am UTC](https://erlangforums.com/t/type-of-calls-allowed-inside-erlang-process-sync-async-blocking-nonblocking/1951/5 "2022-10-18T11:47:21Z")

</div>

Thanks Louis. What about Erlang functions that perform IO or Network operations?

---

<div class="post-metadata">

**Author:** ![Maria-12648430](https://erlangforums.com/user_avatar/erlangforums.com/maria-12648430/32/604_2.png) [@Maria-12648430](https://erlangforums.com/u/Maria-12648430)\
**Post date:** [October 18, 2022, 11:52am UTC](https://erlangforums.com/t/type-of-calls-allowed-inside-erlang-process-sync-async-blocking-nonblocking/1951/6 "2022-10-18T11:52:51Z")

</div>

Oh, so you are talking about _function_ calls, not calls as a mode of inter-process communication. In that case, they will block unless they are done in a separate process.

---

<div class="post-metadata">

**Author:** ![Maria-12648430](https://erlangforums.com/user_avatar/erlangforums.com/maria-12648430/32/604_2.png) [@Maria-12648430](https://erlangforums.com/u/Maria-12648430)\
**Post date:** [October 18, 2022, 11:54am UTC](https://erlangforums.com/t/type-of-calls-allowed-inside-erlang-process-sync-async-blocking-nonblocking/1951/7 "2022-10-18T11:54:13Z")

</div>

That is, they will block the process doing the operation, not a scheduler or something else deeper in ERTS 😉 Like, in the same way as calling `timer:sleep(1000)` will “block” the calling process until the function call returns.

---

<div class="post-metadata">

**Author:** ![lpil](https://erlangforums.com/user_avatar/erlangforums.com/lpil/32/222_2.png) [@lpil](https://erlangforums.com/u/lpil)\
**Post date:** [October 18, 2022, 12:30pm UTC](https://erlangforums.com/t/type-of-calls-allowed-inside-erlang-process-sync-async-blocking-nonblocking/1951/8 "2022-10-18T12:30:48Z")

</div>

> [@mohan](#):
>
> What about Erlang functions that perform IO or Network operations?

Yup, as @Maria-12648430 says none of it will block the scheduler or OS threads and can safely be used at any time, including functions that perform IO.

The only exception I can think of is some of the functions in the [persistent\_term module](https://www.erlang.org/doc/man/persistent_term.html) which will trigger global garbage collection run. (Garbage collection is normally per-process and does not involve a global pause).

---

<div class="post-metadata">

**Author:** ![mohan](https://erlangforums.com/user_avatar/erlangforums.com/mohan/32/1657_2.png) [@mohan](https://erlangforums.com/u/mohan)\
**Post date:** [October 18, 2022, 1:16pm UTC](https://erlangforums.com/t/type-of-calls-allowed-inside-erlang-process-sync-async-blocking-nonblocking/1951/9 "2022-10-18T13:16:35Z")

</div>

Thanks Maria & Louis for clarifying that making blocking calls inside a Process blocks only the process, not the scheduler or the OS threads.

---

<div class="post-metadata">

**Author:** ![mohan](https://erlangforums.com/user_avatar/erlangforums.com/mohan/32/1657_2.png) [@mohan](https://erlangforums.com/u/mohan)\
**Post date:** [October 18, 2022, 1:17pm UTC](https://erlangforums.com/t/type-of-calls-allowed-inside-erlang-process-sync-async-blocking-nonblocking/1951/10 "2022-10-18T13:17:33Z")

</div>

May I request someone to clarify the following query?

If there is any material on Erlang best practices, patterns or anti-pattern, please point me to them.

---

<div class="post-metadata">

**Author:** ![lpil](https://erlangforums.com/user_avatar/erlangforums.com/lpil/32/222_2.png) [@lpil](https://erlangforums.com/u/lpil)\
**Post date:** [October 18, 2022, 1:39pm UTC](https://erlangforums.com/t/type-of-calls-allowed-inside-erlang-process-sync-async-blocking-nonblocking/1951/11 "2022-10-18T13:39:24Z")

</div>

I’m a big fan of this book, though it’s a bit advanced. There might be some better foundational reading.

> **[Designing for Scalability with Erlang/OTP](https://www.oreilly.com/library/view/designing-for-scalability/9781449361556/)**
>
> If you need to build a scalable, fault tolerant system with requirements for high availability, discover why the Erlang/OTP platform stands out for the breadth, depth, and consistency of its … - Selection from Designing for Scalability with...

---

<div class="post-metadata">

**Author:** ![max-au](https://erlangforums.com/user_avatar/erlangforums.com/max-au/32/1627_2.png) [@max-au](https://erlangforums.com/u/max-au)\
**Post date:** [October 18, 2022, 8:24pm UTC](https://erlangforums.com/t/type-of-calls-allowed-inside-erlang-process-sync-async-blocking-nonblocking/1951/12 "2022-10-18T20:24:06Z")

</div>

These operations exist in both blocking and non-blocking variants. Example with the `socket` API:

```erlang
case socket:send(Socket, Data) of
  {ok, RestData} ->
    %% RestData is what has not been sent, but we haven't been blocked

```

You may want to choose blocking call, e.g. `socket:send/3`, that has a timeout. Calling process will be suspended for up to that amount of time. However this will not block the OS thread (unlike in Java).

It’s not an anti-pattern in Erlang to do a blocking call. In fact this allows to simplify programming model.
