# Threads spawned in Graph init

**URL:** <https://igraph.discourse.group/t/threads-spawned-in-graph-init/1617>\
**Category:** Usage\
**Tags:** Python\
**Created:** [19 July 2023 11:42 UTC](https://igraph.discourse.group/t/threads-spawned-in-graph-init/1617 "2023-07-19T11:42:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eyal\_Dayan](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/eyal_dayan/32/972_2.png) [@Eyal\_Dayan](https://igraph.discourse.group/u/Eyal_Dayan)\
**Post date:** [19 July 2023 11:42 UTC](https://igraph.discourse.group/t/threads-spawned-in-graph-init/1617/1 "2023-07-19T11:42:52Z")

</div>

Calling Graph() spawns 11 threads.  
In order to avoid spawning these threads, I found that calling GraphBase() does not spawn them.

Should I expect any degradation in performance if I use GraphBase rather than Graph? The only APIs I’m using are `add_edges, add_vertices, and `topological\_sorting`

Thanks

```auto
In [1]: import psutil
   ...: p = psutil.Process()

In [2]: p.num_threads()
Out[2]: 2

In [3]: from igraph import Graph, GraphBase

In [4]: g = GraphBase()

In [5]: p.num_threads()
Out[5]: 2

In [6]: g = Graph()

In [7]: p.num_threads()
Out[7]: 13

```

---

<div class="post-metadata">

**Author:** ![szhorvat](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/szhorvat/32/3_2.png) [@szhorvat](https://igraph.discourse.group/u/szhorvat)\
**Post date:** [19 July 2023 12:13 UTC](https://igraph.discourse.group/t/threads-spawned-in-graph-init/1617/2 "2023-07-19T12:13:33Z")

</div>

> [@Eyal\_Dayan](#):
>
> In order to avoid spawning these threads, I found that calling GraphBase() does not spawn them.

Why are you concerned about the number of threads in the Python process?

I believe these are due to loading of external libraries and their initialization.

The normal way to use igraph is to operate with `Graph()`, just as the [documentation states](https://python.igraph.org/en/stable/api/igraph.GraphBase.html). `GraphBase` is essentially an implementation detail.

* * *

We might be able to give a better answer if you ask about the actual problem you are having, in the spirit of [https://xyproblem.info/](https://xyproblem.info/) Are you looking to control the number of _active_ threads in the few parallelized igraph functions? Note that that is not the same as what you see with `p.num_threads()`.

---

<div class="post-metadata">

**Author:** ![Eyal\_Dayan](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/eyal_dayan/32/972_2.png) [@Eyal\_Dayan](https://igraph.discourse.group/u/Eyal_Dayan)\
**Post date:** [19 July 2023 15:03 UTC](https://igraph.discourse.group/t/threads-spawned-in-graph-init/1617/3 "2023-07-19T15:03:53Z")

</div>

@szhorvat , Thanks for the quick response.

> [@szhorvat](#):
>
> Why are you concerned about the number of threads in the Python process?

It was raised as a concern in an internal review, and it got me wondering. I tend to agree that the effect if negligible.

Investigating a bit further, it seems like the extra threads are coming from numpy.

```auto
In [1]: import psutil

In [2]: p = psutil.Process()

In [3]: p.num_threads()
Out[3]: 2

In [4]: from numpy import ndarray, matrix

In [5]: p.num_threads()
Out[5]: 13

```

---

<div class="post-metadata">

**Author:** ![szhorvat](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/szhorvat/32/3_2.png) [@szhorvat](https://igraph.discourse.group/u/szhorvat)\
**Post date:** [19 July 2023 15:21 UTC](https://igraph.discourse.group/t/threads-spawned-in-graph-init/1617/4 "2023-07-19T15:21:32Z")

</div>

Most of igraph is not parallelized yet.

The exceptions are:

- PageRank with the PRPACK method uses OpenMP, and so does the power law fitting. OpenMP must be enabled at compilation time, which will not be the case for all binary distributions. As I recall, it _is_ enabled for the PyPI binaries. You can control the number of threads with the `OMP_NUM_THREADS` environment variable.
- The BLAS implementation that igraph is linked to _may_ be parallelized. How to control it differs between BLAS implementations. The python-igraph on PyPI does not currently use a parallelized BLAS.

---

<div class="post-metadata">

**Author:** ![szhorvat](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/szhorvat/32/3_2.png) [@szhorvat](https://igraph.discourse.group/u/szhorvat)\
**Post date:** [12 November 2023 22:38 UTC](https://igraph.discourse.group/t/threads-spawned-in-graph-init/1617/5 "2023-11-12T22:38:59Z")

</div>

A post was split to a new topic: [How to tell if igraph was compiled with OpenMP?](https://igraph.discourse.group/t/how-to-tell-if-igraph-was-compiled-with-openmp/1702)
