# Jython multi-threading

**URL:** <https://forum.inductiveautomation.com/t/jython-multi-threading/34770>\
**Category:** Ignition\
**Tags:** ignition79\
**Created:** [May 14, 2020, 11:29pm UTC](https://forum.inductiveautomation.com/t/jython-multi-threading/34770 "2020-05-14T23:29:55Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matrix\_Engineering](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/matrix_engineering/32/19781_2.png) [@Matrix\_Engineering](https://forum.inductiveautomation.com/u/Matrix_Engineering)\
**Post date:** [May 14, 2020, 11:29pm UTC](https://forum.inductiveautomation.com/t/jython-multi-threading/34770/1 "2020-05-14T23:29:55Z")

</div>

Am I correct in saying that a call to InvokeAsync will call to just 1 thread of the CPU?

Therefore if I had an intensive script that had 5 heavy functions, but all 5 functions could run at the same time, my best bet for minimum overall execution time is to call 5 Async’s at the same time?

---

<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:** [May 15, 2020, 12:16am UTC](https://forum.inductiveautomation.com/t/jython-multi-threading/34770/2 "2020-05-15T00:16:03Z")

</div>

Yes.

---

<div class="post-metadata">

**Author:** ![Matrix\_Engineering](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/matrix_engineering/32/19781_2.png) [@Matrix\_Engineering](https://forum.inductiveautomation.com/u/Matrix_Engineering)\
**Post date:** [May 15, 2020, 12:18am UTC](https://forum.inductiveautomation.com/t/jython-multi-threading/34770/3 "2020-05-15T00:18:15Z")

</div>

Thanks Phil. I take it it would be rather silly to call 20 Async’s on a 16 thread CPU? Not that I am going to or need to, but what would happen, it would queue 4 until a thread becomes available? But also dangerous because the call might affect the main GUI thread?

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [May 15, 2020, 12:37am UTC](https://forum.inductiveautomation.com/t/jython-multi-threading/34770/4 "2020-05-15T00:37:30Z")

</div>

No, threads aren’t tied or limited to CPUs. You may notice a slowdown but all will run effectively at once.

---

<div class="post-metadata">

**Author:** ![Matrix\_Engineering](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/matrix_engineering/32/19781_2.png) [@Matrix\_Engineering](https://forum.inductiveautomation.com/u/Matrix_Engineering)\
**Post date:** [May 15, 2020, 1:06am UTC](https://forum.inductiveautomation.com/t/jython-multi-threading/34770/5 "2020-05-15T01:06:17Z")

</div>

Interesting, what is it limited by?

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [May 15, 2020, 1:14am UTC](https://forum.inductiveautomation.com/t/jython-multi-threading/34770/6 "2020-05-15T01:14:39Z")

</div>

Technically… the stack size, but in practice you’ll probably find the effective limit sooner…

---

<div class="post-metadata">

**Author:** ![Matrix\_Engineering](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/matrix_engineering/32/19781_2.png) [@Matrix\_Engineering](https://forum.inductiveautomation.com/u/Matrix_Engineering)\
**Post date:** [May 15, 2020, 1:23am UTC](https://forum.inductiveautomation.com/t/jython-multi-threading/34770/7 "2020-05-15T01:23:22Z")

</div>

OK (as in, my ignorance doesn’t know what you’re referring to…)

Overall system (gateway computer) spec will help though, right?

I have two test workstations here; 1 I9-9900K and 1 I7-5960X. You’ve piqued my curiosity now and I will run the same script on both, with script execution time logging.

---

<div class="post-metadata">

**Author:** ![PRAMANJ](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pramanj/32/66935_2.png) [@PRAMANJ](https://forum.inductiveautomation.com/u/PRAMANJ)\
**Post date:** [May 15, 2020, 4:12am UTC](https://forum.inductiveautomation.com/t/jython-multi-threading/34770/8 "2020-05-15T04:12:23Z")

</div>

Number of threads depends on JAVA and the underlying OS not Jython and I think we need not be over concerned about it as developers as long as we are implementing some reasonable features with it! It also depends upon whether you are using it on client side or server side, lest you should block the server it self due to some bug it our code or implementing some compute intensive or blocking code ! Of course we should know the system limitations but they are practically unlimited for reasonable requirements.

---

<div class="post-metadata">

**Author:** ![Matrix\_Engineering](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/matrix_engineering/32/19781_2.png) [@Matrix\_Engineering](https://forum.inductiveautomation.com/u/Matrix_Engineering)\
**Post date:** [May 15, 2020, 4:23am UTC](https://forum.inductiveautomation.com/t/jython-multi-threading/34770/9 "2020-05-15T04:23:20Z")

</div>

I9: 8 core, 16 thread, 16GB RAM, Win 10 Pro, Ignition 7.9.13, 1.8.0\_221-b11  
I7: 8 core, 16 thread, 32GB RAM, Debian Stretch 9 Headless, Docker, 7.9.13, 1.8.0\_242-b08

Server side, Gateway scope

---

<div class="post-metadata">

**Author:** ![Sanderd17](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/sanderd17/32/8735_2.png) [@Sanderd17](https://forum.inductiveautomation.com/u/Sanderd17)\
**Post date:** [May 15, 2020, 6:27am UTC](https://forum.inductiveautomation.com/t/jython-multi-threading/34770/10 "2020-05-15T06:27:02Z")

</div>

> [@Matrix\_Engineering](#):
>
> Thanks Phil. I take it it would be rather silly to call 20 Async’s on a 16 thread CPU? Not that I am going to or need to, but what would happen, it would queue 4 until a thread becomes available? But also dangerous because the call might affect the main GUI thread?

If you're interested in that topic, it's called CPU scheduling. It's effectively the responsibility of the OS, but applications can have an influence on it (especially to schedule internal threads like in the JVM).

Basically the OS will try to distribute the CPU time evenly over the different threads that need it, without waiting too long, and without causing too much context switches (context switches have even become more costly due to spectre, meltdown and similar CPU bugs). Those requirements are efectively contradictory: the longer you wait to switch, the less evenly you can distribute the time and the longer threads have to wait.

In Linux, these are all [options you can configure](https://doc.opensuse.org/documentation/leap/archive/42.1/tuning/html/book.sle.tuning/cha.tuning.taskscheduler.html) to get your ideal working.

But default settings should be generic enough to have a good performance in most cases. You really need to study those options a lot and test them with trial-and-error to get a better performance from it.

---

<div class="post-metadata">

**Author:** ![Matrix\_Engineering](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/matrix_engineering/32/19781_2.png) [@Matrix\_Engineering](https://forum.inductiveautomation.com/u/Matrix_Engineering)\
**Post date:** [May 15, 2020, 6:45am UTC](https://forum.inductiveautomation.com/t/jython-multi-threading/34770/11 "2020-05-15T06:45:38Z")

</div>

> [@Sanderd17](#):
>
> In Linux, these are all [options you can configure](https://doc.opensuse.org/documentation/leap/archive/42.1/tuning/html/book.sle.tuning/cha.tuning.taskscheduler.html) to get your ideal working.

Good to know because the final destination will be the Linux box.

I'll post some results of the execution times between the two, the I9 has a higher clock speed which might make up for Windoze issues

---

<div class="post-metadata">

**Author:** ![Sanderd17](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/sanderd17/32/8735_2.png) [@Sanderd17](https://forum.inductiveautomation.com/u/Sanderd17)\
**Post date:** [May 15, 2020, 7:18am UTC](https://forum.inductiveautomation.com/t/jython-multi-threading/34770/12 "2020-05-15T07:18:40Z")

</div>

> [@Sanderd17](#):
>
> But default settings should be generic enough to have a good performance in most cases. You really need to study those options a lot and test them with trial-and-error to get a better performance from it.

In case this wasn't clear, it's actually a rabbit hole. It's nice you can do those configurations. But unless it's really needed (like for supercomputer applications where every added bit of performance has high value), you'll likely to lose more time configuring it than you will ever gain from it.

---

<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:** [May 15, 2020, 10:34am UTC](https://forum.inductiveautomation.com/t/jython-multi-threading/34770/13 "2020-05-15T10:34:32Z")

</div>

Also note that unless your individual tasks are truly compute-only, running them all simultaneously will allow the OS to use more of the idle time around file and network operations.

If the _are_ compute-only, and you disturb the rest of the system, consider using a thread pool executor to limit the number of simultaneous threads.

---

<div class="post-metadata">

**Author:** ![Matrix\_Engineering](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/matrix_engineering/32/19781_2.png) [@Matrix\_Engineering](https://forum.inductiveautomation.com/u/Matrix_Engineering)\
**Post date:** [May 15, 2020, 10:39am UTC](https://forum.inductiveautomation.com/t/jython-multi-threading/34770/14 "2020-05-15T10:39:35Z")

</div>

They will call a store and forward DB insert, lot’s of rows

---

<div class="post-metadata">

**Author:** ![Sanderd17](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/sanderd17/32/8735_2.png) [@Sanderd17](https://forum.inductiveautomation.com/u/Sanderd17)\
**Post date:** [May 15, 2020, 10:59am UTC](https://forum.inductiveautomation.com/t/jython-multi-threading/34770/15 "2020-05-15T10:59:46Z")

</div>

That’s IO intensive, in which case the CPU won’t be your bottleneck, but your IO devices (network, disk, …) will.
