# Nested Query

**URL:** <https://swi-prolog.discourse.group/t/nested-query/4098>\
**Category:** Help!\
**Tags:** how-to\
**Created:** [June 22, 2021, 8:55am UTC](https://swi-prolog.discourse.group/t/nested-query/4098 "2021-06-22T08:55:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ferdasonmez](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/ferdasonmez/32/3578_2.png) [@ferdasonmez](https://swi-prolog.discourse.group/u/ferdasonmez)\
**Post date:** [June 22, 2021, 8:55am UTC](https://swi-prolog.discourse.group/t/nested-query/4098/1 "2021-06-22T08:55:58Z")

</div>

Hi,

I am calling one Prolog function which returns a list. I traverse the list using a Python loop and do somethings. In the same loop, when I catch a specific condition, I call another Prolog function. I tried to execute this code with a single prolog object but got

> “pyswip.prolog.NestedQueryError: The last query was not closed”

error. Later, I decided to use a second prolog variable for inner queries. Even, having two totally independent prolog objects (each having its own assertions and consultation calls), I am still getting the same error. Can somebody show me how to do this?

Many thanks, Ferda

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [June 22, 2021, 10:10am UTC](https://swi-prolog.discourse.group/t/nested-query/4098/2 "2021-06-22T10:10:06Z")

</div>

This is also [posted](https://stackoverflow.com/q/68072285/1243762) on StackOverflow.  
This is also talked about in this [reply](https://swi-prolog.discourse.group/t/multi-threading-when-using-swi-prolog-as-embeded-engine/2085/10) of another topic.
