# Is there a pengine\_input() sample I could look at?

**URL:** <https://swi-prolog.discourse.group/t/is-there-a-pengine-input-sample-i-could-look-at/1977>\
**Category:** Predicate\
**Created:** [March 7, 2020, 7:13am UTC](https://swi-prolog.discourse.group/t/is-there-a-pengine-input-sample-i-could-look-at/1977 "2020-03-07T07:13:37Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![prodog](https://avatars.discourse-cdn.com/v4/letter/p/57b2e6/32.png) [@prodog](https://swi-prolog.discourse.group/u/prodog)\
**Post date:** [March 7, 2020, 7:13am UTC](https://swi-prolog.discourse.group/t/is-there-a-pengine-input-sample-i-could-look-at/1977/1 "2020-03-07T07:13:37Z")

</div>

I would like to study a code sample that uses the `pengine_input` predicate that shows how you return input to the server from the client code side. My main question is what method do I use on the client side Javascript code to return the pending input to the server?

Also, if there ends up being a large number of pending input requests, could that affect negatively the server as far as thread usage or memory is concerned? I am worried that scenario could arise if I start doing asynchronous operations in my Node.js server that is the client to my PEngines server, that are part of the processing chain. For example, what happens if I get a large number of the following events:

- A client side request for _user A_ from browser occurs
- PEngines code executes a `pengine_input` call (_user A_)
- My Node.JS server executes an asynchronous call to an outbound server or perhaps even to a human (i.e. - a very slow response situation)
- While waiting for a response, client request for _user B_ occurs
- PEngines code executes a `pengine_input` call (_user B)_  
…

And so on and so on. So in the case of a response being dependent on a large number of external, slow services, I worry that the interleaved operations might lead to a “pile up” of open threads or SWI-Prolog resources associated with the pending `pengine_input` calls, and I am wondering if that situation could cause problems. Is this possible?

[https://www.swi-prolog.org/pldoc/doc\_for?object=pengine\_input/2](https://www.swi-prolog.org/pldoc/doc_for?object=pengine_input/2)

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [March 8, 2020, 4:00pm UTC](https://swi-prolog.discourse.group/t/is-there-a-pengine-input-sample-i-could-look-at/1977/2 "2020-03-08T16:00:26Z")

</div>

> [@prodog](#):
>
> how you return input to the server from the client code side

The most extensive example is probably the SWISH server. See `web/js/runner.js`. And yes, you can create all sorts of loops that will blow up the server. That is simply a fact for services that can contact other services. What to do against that depends a lot on the setup.

---

<div class="post-metadata">

**Author:** ![prodog](https://avatars.discourse-cdn.com/v4/letter/p/57b2e6/32.png) [@prodog](https://swi-prolog.discourse.group/u/prodog)\
**Post date:** [March 9, 2020, 6:04am UTC](https://swi-prolog.discourse.group/t/is-there-a-pengine-input-sample-i-could-look-at/1977/3 "2020-03-09T06:04:07Z")

</div>

Thanks Jan. Is there a number or set of numbers I should watch closely in the PEngines admin panel that will help me estimate the amount of memory one of my PEngines sessions is holding onto while waiting for a pending `pegine_input()` response? If there is/are, I’ll set up a test case where I launch a bunch of Node.JS calls into my PEngines server that will all trigger a `pengine_input()` call. I won’t respond to this call on purpose, just to see how the numbers change.

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [March 9, 2020, 7:56am UTC](https://swi-prolog.discourse.group/t/is-there-a-pengine-input-sample-i-could-look-at/1977/4 "2020-03-09T07:56:55Z")

</div>

Pengines has become a bit overloaded term. I think you are talking about [this repo](https://github.com/SWI-Prolog/pengines). Be a bit careful with this code as it is not maintained. The core Pengines are distributed with SWI-Prolog itself and resides [here](https://github.com/SWI-Prolog/packages-pengines). I.e., to run a Pengine server you do not need anything but the Prolog release. Then there is SWISH, which started of as an application for the original pengines web app, but which grew into being a stand alone project. The core pengines as distributed with Prolog and SWISH are both considered key features of the system and are actively maintained. Finally there is @torbjorn.lager’s new Web Prolog.

For resource tracking I’m working on [webstat](https://github.com/SWI-Prolog/webstat), which should eventually become a complete web based dashboard to track what is going on inside a SWI-Prolog process. There is a long way to go … Until then your tools are the OS monitoring tools, statistics/0, the xpce based thread monitor and zillions of predicates that can tell you details about the state of the system but that are scattered al over the place and hard to use (that is what webstat should solve).

---

<div class="post-metadata">

**Author:** ![prodog](https://avatars.discourse-cdn.com/v4/letter/p/57b2e6/32.png) [@prodog](https://swi-prolog.discourse.group/u/prodog)\
**Post date:** [March 9, 2020, 12:36pm UTC](https://swi-prolog.discourse.group/t/is-there-a-pengine-input-sample-i-could-look-at/1977/5 "2020-03-09T12:36:50Z")

</div>

Is WebStat only for tabled apps? Or can I use it with mine, which does not use SWI-Prolog’s table feature?

> **[SWI-Prolog/webstat](https://github.com/SWI-Prolog/webstat)**
>
> Examine SWI-Prolog resources and data using your browser - SWI-Prolog/webstat

## Running

- **Load your (tabled) project**
- Load `server.pl` from this package
- Run this to start the server on port 4000?- webstat(4000).
- Direct your browser to [http://localhost:4000](http://localhost:4000/)

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [March 9, 2020, 12:51pm UTC](https://swi-prolog.discourse.group/t/is-there-a-pengine-input-sample-i-could-look-at/1977/6 "2020-03-09T12:51:59Z")

</div>

> [@prodog](#):
>
> Is WebStat only for tabled apps? Or can I use it with mine, which does not use SWI-Prolog’s table feature?

There is some stuff of general interest such as overall resource usage and profiling (though currently only of the main thread). Most is about tabling as its development started to gain insight about what was going on in a large tabled program.

---

<div class="post-metadata">

**Author:** ![prodog](https://avatars.discourse-cdn.com/v4/letter/p/57b2e6/32.png) [@prodog](https://swi-prolog.discourse.group/u/prodog)\
**Post date:** [March 13, 2020, 3:03pm UTC](https://swi-prolog.discourse.group/t/is-there-a-pengine-input-sample-i-could-look-at/1977/7 "2020-03-13T15:03:10Z")

</div>

Hi Jan,

Any thoughts on identifying orphan PEngine sessions? Here is the scenario I want to handle:

- My Node.JS server queries My PEngines server (i.e. - SWI-Prolog using its PEngines features, aka the “main” repo)
- The PEngine session executes a _pengine\_input()_ request to my Node.JS server
- My Node.JS server never responds, due to a crash, an Exception, etc.
- Now the PEngine session is an “orphan”

Is there a facility in Prolog/PEngines to detect a sessions that are stale? For example, some way to scan for any open sessions that have not had any activity in X seconds?

If not, would a viable approach be as follow:

- From inside my Prolog code, create a wrapper predicate called something like _guard\_pengine\_input()_
- That predicate asserts the session ID and the current date/time before issuing the _pengine\_input()_ call. For example “assert(pengine\_input\_called(InstanceId, Date)).”
- Retract that from the dynamic database when the session that issued the _pengine\_input()_ call concludes.
- Periodically scan the dynamic data base using the query “pengine\_input\_called(InstanceId, Date).”. Kill any sessions whose Date value is over X seconds old.

If that is a viable strategy, what is the best mechanism for periodically scanning the dynamic database? Is there a native SWI-Prolog predicate for setting up something like an internal cron job? If not, then should I set up a predicate marked as “safe” for PEngine use that my Node.JS server calls periodically on a Javascript interval, and have this query do that orphan detection work?

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [March 14, 2020, 3:46pm UTC](https://swi-prolog.discourse.group/t/is-there-a-pengine-input-sample-i-could-look-at/1977/9 "2020-03-14T15:46:36Z")

</div>

Pengines are protected by two time limits: one on wall time while computing and one on wall time while waiting. If either elapses the Pengine stops. See pengines.pl settings as well as SWISH `debug.pl` plugin.

---

<div class="post-metadata">

**Author:** ![prodog](https://avatars.discourse-cdn.com/v4/letter/p/57b2e6/32.png) [@prodog](https://swi-prolog.discourse.group/u/prodog)\
**Post date:** [March 14, 2020, 5:34pm UTC](https://swi-prolog.discourse.group/t/is-there-a-pengine-input-sample-i-could-look-at/1977/10 "2020-03-14T17:34:04Z")

</div>

Great! So I don’t have to worry about this. Easiest upgrade ever. 🙂
