# More thread wrangling

**URL:** <https://swi-prolog.discourse.group/t/more-thread-wrangling/9155>\
**Category:** General\
**Created:** [August 4, 2025, 11:58pm UTC](https://swi-prolog.discourse.group/t/more-thread-wrangling/9155 "2025-08-04T23:58:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![m\_ac](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/m_ac/32/6223_2.png) [@m\_ac](https://swi-prolog.discourse.group/u/m_ac)\
**Post date:** [August 4, 2025, 11:58pm UTC](https://swi-prolog.discourse.group/t/more-thread-wrangling/9155/1 "2025-08-04T23:58:23Z")

</div>

Hi, [this is a follow up to my previous post about this](https://swi-prolog.discourse.group/t/help-getting-up-and-running-with-multithreading/7378/20). Now that I’m a little more practiced in multithreading, I would like to reset a bit on this question if I may.

Again, what I’m trying to accomplish:

1. [Paginate](https://dev.to/pragativerma18/unlocking-the-power-of-api-pagination-best-practices-and-strategies-4b49) through API calls.
2. Abide by a rate limit.
3. Execute page calls:
  1. in order
  2. asynchronously, up until the rate limit.

On the previous post I was given advice which ultimately helped me accomplish my goal (thank you!). The advice was: If you have units of work `[1, 2, 3, 4, 5, 6]` then “bucketize“ the work per the rate limit, so if it’s 3 you should get `[[1, 2, 3],[4, 5, 6]]` , and then iterate through each bucket, executing each concurrently and sleeping between each bucket.

I used concurrent/3 for this and it worked like a charm.

The only reason this worked though is because the API I’m calling _happens_ to provide a `totalPages` property so, very roughly, that enabled me to do something like

```prolog
get_things([]).
get_things([Bucket|Buckets]) :-
  concurrent(3,Bucket,[]),
  sleep(Rate_limit_sleep),
  get_things(Buckets).

get_things_wrapper(Things) :-
  api_call(get_things,Response),
  Total_pages = Response.totalPages,
  bucketize_pages(Total_pages,Page_buckets),
  get_things(Page_buckets).

```

What I’m wondering though, is what if I was _not_ provided the total pages beforehand?

What if instead of `totalPages` the response only provides a boolean `is_last` property which lets you know when you’ve paginated to the last page. In that case you can’t bucketize as above because you don’t know how many buckets to create and you would have to process each page as you get to it to check for that property.

**So I’m trying to get this minimal example to work:**

```prolog
rate_limit(Count,New_count) :-
  succ(Count,Count_),
  once((
     Count_ = 3,
     writeln("wait"),
     sleep(3),
     New_count = 0
     ;
     New_count = Count_
   )).

work(Work) :-
  %Network_latency = 2,
  %sleep(Network_latency),
  writeln(Work).

do_work(_,[]) :- !.
do_work(C,[Work|Works]) :-
  thread_create(work(Work),_),
  rate_limit(C,C0),
  do_work(C0,Works).

main :-
  numlist(1,12,Work),
  do_work(0,Work).

```

(in this example we _happen_ to know there are 12 units of work but again, assume you _don’t_ know that in advance.)

Ignoring the “network latency” for now, what I would naively _expect_ running main here is `1, 2, 3, wait(3s), 4, 5, 6, wait(3s)...`, and so on.

Instead what I’m getting is a variety of wonky output like `wait 1, 2, 3, 4, 5, 6, wait, 7, wait, 8, 9, 10, 11, wait, 12` or `1, 2, wait, 3, 4, wait, 5, 6, wait, 7, 9, 8, 10, 11, wait, 12`.

From my surface level understanding I believe this because we are offloading the threads to the OS but we’re cycling through `do_work/2` so fast that the OS isn’t able to schedule them before we hit the rate limit.

Now uncomment the two latency lines in `work/1` and run it. From my testing, this now executes concurrently; ie. I only had to wait 2 seconds for all three, not 3 units\_work \* 2 seconds\_each = 6 seconds synchronous wait, but out of order.

Any guidance here is greatly appreciated.

**In short** what I’m trying to do is get my output to appear exactly like, and be as performant as, this

```prolog
rate_limit(Count,New_count) :-
  succ(Count,Count_),
  once((
     Count_ = 10,
     writeln("wait"),
     sleep(3),
     New_count = 0
     ;
     New_count = Count_
   )).

work(Work) :-
  %Network_latency = 2,
  %sleep(Network_latency),
  writeln(Work).

do_work(_,[]) :- !.
do_work(C,[Work|Works]) :-
  work(Work),
  rate_limit(C,C0),
  do_work(C0,Works).

main :-
  numlist(1,100,Work),
  do_work(0,Work).

```

except when you uncomment network latency, I don’t want to wait 2 seconds \* 10 units of work = 20 seconds between rate limits. I want them to execute concurrently so I only wait 2 seconds total.

---

<div class="post-metadata">

**Author:** ![m\_ac](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/m_ac/32/6223_2.png) [@m\_ac](https://swi-prolog.discourse.group/u/m_ac)\
**Post date:** [August 6, 2025, 9:10am UTC](https://swi-prolog.discourse.group/t/more-thread-wrangling/9155/3 "2025-08-06T09:10:02Z")

</div>

Thank you, sir. Checking it out.

I also had to slightly edit my requirements with the recent realization that I actually need the pages called asynchronously but in order, because if I have pages …4,5,6 within a rate limit cycle, and 4 is the last page, I don’t want to call 6 & 5 out of order just because the threads happen to be scheduled that way and waste api calls. I want 4 to be called, see it’s the last page and break.

---

<div class="post-metadata">

**Author:** ![m\_ac](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/m_ac/32/6223_2.png) [@m\_ac](https://swi-prolog.discourse.group/u/m_ac)\
**Post date:** [August 6, 2025, 12:14pm UTC](https://swi-prolog.discourse.group/t/more-thread-wrangling/9155/5 "2025-08-06T12:14:09Z")

</div>

There’s a time, thread, and max requests rate limit, but for now I’m mainly focusing on max requests.

I think I’m coming to the realization that what I’m trying to do is impossible in parallel 🙃

I think best case scenario, if I’m going to base the logic on `is_last`, here’s what I would want:

So by way of example, there’s the [Verse programming language](https://dev.epicgames.com/documentation/en-us/fortnite/verse-language-reference). Verse supports [several types of control flows for concurrency](https://dev.epicgames.com/documentation/en-us/fortnite/time-flow-and-concurrency-in-verse). One of them is called [race](https://dev.epicgames.com/documentation/en-us/fortnite/race-in-verse). What race does is sets up some concurrent threads and after the first thread successfully completes, verse cancels the rest.

I’m wondering if I could do is something like that except instead of basing it on which thread simply completes first, rather it completes with a certain condition, and cancels concurrent threads with a certain condition as well.

Meaning:

Suppose I’m concurrently calling

```prolog
api_call(get_thing,page(4),Response).
api_call(get_thing,page(5),Response).
api_call(get_thing,page(6),Response).

```

and suppose one of these is the last page but we don’t know which one it is.

If page(4) comes back with `Response.is_last = true` then we don’t need pages 5 and 6. If page(5) completes first and comes back with `is_last` then we still need page(4) but we can cancel page(6).

So is there a way that we can cancel threads based on the payload they are executing? ie. page(4) comes back with `is_last` , so all threads with payload `api_call(get_thing,page(N),Response)` where N \> 4 get cancelled?

* * *

Of course, this leads to the possibility that unbeknownst to me page(4) is last but pages 5 and 6 get called first, so 🙃 this may have been a big swing and a miss for me.

I dunno.. Is what I’m trying to do not possible?

Appreciate your input.

---

<div class="post-metadata">

**Author:** ![brebs](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@brebs](https://swi-prolog.discourse.group/u/brebs)\
**Post date:** [August 6, 2025, 12:28pm UTC](https://swi-prolog.discourse.group/t/more-thread-wrangling/9155/6 "2025-08-06T12:28:54Z")

</div>

> [@m\_ac](#):
>
> One of them is called [race](https://dev.epicgames.com/documentation/en-us/fortnite/race-in-verse). What race does is sets up some concurrent threads and after the first thread successfully completes, verse cancels the rest.

first\_solution/3 does that.

---

<div class="post-metadata">

**Author:** ![m\_ac](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/m_ac/32/6223_2.png) [@m\_ac](https://swi-prolog.discourse.group/u/m_ac)\
**Post date:** [August 6, 2025, 12:40pm UTC](https://swi-prolog.discourse.group/t/more-thread-wrangling/9155/7 "2025-08-06T12:40:11Z")

</div>

I’ll call it good and just keep using `totalPages` and hope they don’t deprecate it.

Thanks all!
