# Should I Create a Thread When Handling Time-consuming Task in Message Handler?

**URL:** <https://forum.inductiveautomation.com/t/should-i-create-a-thread-when-handling-time-consuming-task-in-message-handler/65046>\
**Category:** Ignition\
**Created:** [October 11, 2022, 1:58am UTC](https://forum.inductiveautomation.com/t/should-i-create-a-thread-when-handling-time-consuming-task-in-message-handler/65046 "2022-10-11T01:58:44Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jerald.Zhang](https://avatars.discourse-cdn.com/v4/letter/j/74df32/32.png) [@Jerald.Zhang](https://forum.inductiveautomation.com/u/Jerald.Zhang)\
**Post date:** [October 11, 2022, 1:58am UTC](https://forum.inductiveautomation.com/t/should-i-create-a-thread-when-handling-time-consuming-task-in-message-handler/65046/1 "2022-10-11T01:58:44Z")

</div>

What if current time-consuming task does not finish while another task come to message handler?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [October 11, 2022, 12:21pm UTC](https://forum.inductiveautomation.com/t/should-i-create-a-thread-when-handling-time-consuming-task-in-message-handler/65046/2 "2022-10-11T12:21:02Z")

</div>

Message handlers work one-at-a-time, so if you need to perform long-running tasks, you will need to offload them. Running an asynchronous thread is a common solution, as long as your message pattern doesn't make more of them than your gateway can handle. If you need to queue tasks for multiple, but limited threads, you can use java to create a thread pool executor.

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [October 11, 2022, 12:30pm UTC](https://forum.inductiveautomation.com/t/should-i-create-a-thread-when-handling-time-consuming-task-in-message-handler/65046/3 "2022-10-11T12:30:12Z")

</div>

> [@pturmel](#):
>
> Message handlers work one-at-a-time

So what happens when the same message is caught by more than one handler ? Is there a way to know which one will execute first ?  
Is that one handler at a time gateway-wide, or is the scope smaller ?

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [October 11, 2022, 3:14pm UTC](https://forum.inductiveautomation.com/t/should-i-create-a-thread-when-handling-time-consuming-task-in-message-handler/65046/4 "2022-10-11T15:14:26Z")

</div>

Phil's talking about gateway message handlers. I think you're talking about Perspective message handlers, which work differently.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [October 11, 2022, 4:57pm UTC](https://forum.inductiveautomation.com/t/should-i-create-a-thread-when-handling-time-consuming-task-in-message-handler/65046/5 "2022-10-11T16:57:39Z")

</div>

> [@paul-griffith](#):
>
> which work differently.

It would be good to document the constraints there, too. IIRC it's a queue per session?

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [October 11, 2022, 5:38pm UTC](https://forum.inductiveautomation.com/t/should-i-create-a-thread-when-handling-time-consuming-task-in-message-handler/65046/6 "2022-10-11T17:38:09Z")

</div>

Internally the event sending/receiving mechanism is different and likely lower latency, but the actual execution thread will be one from the 'worker' pool. You can configure the pools for blocking and non-blocking work separately:  
[Gateway Configuration File Reference - Ignition User Manual 8.1 - Ignition Documentation](https://docs.inductiveautomation.com/display/DOC81/Gateway+Configuration+File+Reference#GatewayConfigurationFileReference-PerspectiveWorkerPoolRestrictions) (until the docs are updated, use `perspective-queue` in the system props to adjust the blocking work thread pool)

Perspective message handlers are listening on a 'queue' thread, but automatically invoke their attached scripts on a 'worker' thread.

---

<div class="post-metadata">

**Author:** ![Jerald.Zhang](https://avatars.discourse-cdn.com/v4/letter/j/74df32/32.png) [@Jerald.Zhang](https://forum.inductiveautomation.com/u/Jerald.Zhang)\
**Post date:** [October 12, 2022, 3:04am UTC](https://forum.inductiveautomation.com/t/should-i-create-a-thread-when-handling-time-consuming-task-in-message-handler/65046/7 "2022-10-12T03:04:15Z")

</div>

Got it, Turmel, thanks.

---

<div class="post-metadata">

**Author:** ![Jerald.Zhang](https://avatars.discourse-cdn.com/v4/letter/j/74df32/32.png) [@Jerald.Zhang](https://forum.inductiveautomation.com/u/Jerald.Zhang)\
**Post date:** [October 14, 2022, 3:18am UTC](https://forum.inductiveautomation.com/t/should-i-create-a-thread-when-handling-time-consuming-task-in-message-handler/65046/8 "2022-10-14T03:18:15Z")

</div>

Since scripts in message handler is stateless (can't keep context), how to keep a thread pool among messages? Do your mean that unloading long-running tasks and send to a client project?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [October 14, 2022, 12:02pm UTC](https://forum.inductiveautomation.com/t/should-i-create-a-thread-when-handling-time-consuming-task-in-message-handler/65046/9 "2022-10-14T12:02:39Z")

</div>

Top level variables in project script modules hold state until the projected is edited and saved. State can be held longer (with special care) using `system.util.getGlobals()`. I suggest using the latter to hold a single thread pool executor. (If you need to replace it, shut down the one before it.)

Search this forum for `getGlobals` for our accumulated wisdom. (:

---

<div class="post-metadata">

**Author:** ![Jerald.Zhang](https://avatars.discourse-cdn.com/v4/letter/j/74df32/32.png) [@Jerald.Zhang](https://forum.inductiveautomation.com/u/Jerald.Zhang)\
**Post date:** [February 16, 2023, 2:34am UTC](https://forum.inductiveautomation.com/t/should-i-create-a-thread-when-handling-time-consuming-task-in-message-handler/65046/10 "2023-02-16T02:34:07Z")

</div>

I find that in [Gateway Event Scripts] [Gateway Event Scripts - Ignition User Manual 8.1 - Ignition Documentation](https://docs.inductiveautomation.com/display/DOC81/Gateway+Event+Scripts))：

- **Threading** - Determines the threading for the message handler. Contains the following options:
  - **Shared** - The default way of running a message handler. Will execute the handler on a shared pool of threads in the order that they are invoked. If too many message handlers are called all at once and they take long periods of time to execute, there may be delays before each message handler gets to execute.
  - **Dedicated** - The message handler will run on its own dedicated thread. This is useful when a message handler will take a long time to execute, so that it does not hinder the execution of other message handlers. Threads have a bit of overhead, so this option uses more of the Gateway's resources, but is desirable if you want the message handler to not be impeded by the execution of other message handlers.

So, message handler is excuted on a thread pool, and you don't have to create a thread to handle your scripts

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [February 16, 2023, 1:20pm UTC](https://forum.inductiveautomation.com/t/should-i-create-a-thread-when-handling-time-consuming-task-in-message-handler/65046/11 "2023-02-16T13:20:31Z")

</div>

> [@Jerald.Zhang](#):
>
> So, message handler is excuted on a thread pool, and you don't have to create a thread to handle your scripts

This is true in the common case, but only when there's no other state that needs to carry over from one message to another, and you **want** the singulation of message handling.
