# I don't understand how Coroutining works

**URL:** https://swi-prolog.discourse.group/t/i-dont-understand-how-coroutining-works/6345
**Category:** General
**Tags:** how-to
**Created:** [March 6, 2023, 2:26am UTC](https://swi-prolog.discourse.group/t/i-dont-understand-how-coroutining-works/6345 "2023-03-06T02:26:02Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![heiheshang](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/heiheshang/32/4419_2.png) [@heiheshang](https://swi-prolog.discourse.group/u/heiheshang)
#### Post date: [March 6, 2023, 2:26am UTC](https://swi-prolog.discourse.group/t/i-dont-understand-how-coroutining-works/6345/1 "2023-03-06T02:26:02Z")

</div>

I have a web server. Before starting the web server, I do engine\_create(\_, busting\_transactions(0),\_E)

```prolog
busting_transactions(Start) :-
    repeat,
    debug(log , '!!!! ~q', [Start]),
    sleep(60).

```

and I don’t see anything in the console.how to run a program that will run parallel to the web server?

---

<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 6, 2023, 7:46am UTC](https://swi-prolog.discourse.group/t/i-dont-understand-how-coroutining-works/6345/2 "2023-03-06T07:46:28Z")

</div>

Use thread\_create/2,3. An engine requires a thread to make it do something (see engine\_next/2).

---

<div class="post-metadata">

### Author: ![heiheshang](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/heiheshang/32/4419_2.png) [@heiheshang](https://swi-prolog.discourse.group/u/heiheshang)
#### Post date: [March 6, 2023, 7:57am UTC](https://swi-prolog.discourse.group/t/i-dont-understand-how-coroutining-works/6345/3 "2023-03-06T07:57:07Z")

</div>

I thought the engines are an easier alternative to the thread OS. I have a task in the cycle to receive data portions through HTTP\_POST, I think that I can use engines for this. How could their use look like for me?

---

<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 6, 2023, 9:17am UTC](https://swi-prolog.discourse.group/t/i-dont-understand-how-coroutining-works/6345/4 "2023-03-06T09:17:34Z")

</div>

Whether they are easier or not depends on what you want to do. But yes, you can have an engine and have the HTTP handler of the POST request ask the engine to process the request. That way, all requests processed by that engine are _serialized_, i.e., a request that comes and that tries to get the engine will have to wait until the ongoing request is fully processed and the engine is released.

Study the manual pages to see how you can interact with engines. The tests also provide a lot of examples. See [swipl-devel/test\_engines.pl at 2ba73f73c0bc173164b7d45c8a4d1596ddbb2716 · SWI-Prolog/swipl-devel · GitHub](https://github.com/SWI-Prolog/swipl-devel/blob/2ba73f73c0bc173164b7d45c8a4d1596ddbb2716/src/Tests/thread/test_engines.pl)

---

<div class="post-metadata">

### Author: ![heiheshang](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/heiheshang/32/4419_2.png) [@heiheshang](https://swi-prolog.discourse.group/u/heiheshang)
#### Post date: [March 6, 2023, 1:36pm UTC](https://swi-prolog.discourse.group/t/i-dont-understand-how-coroutining-works/6345/6 "2023-03-06T13:36:35Z")

</div>

I made this example to understand how it works. In any case, my program does much more than display a message.
