# Setting Number of Threads at startup for VS Code

**URL:** <https://discourse.julialang.org/t/setting-number-of-threads-at-startup-for-vs-code/63445>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [June 23, 2021, 2:32pm UTC](https://discourse.julialang.org/t/setting-number-of-threads-at-startup-for-vs-code/63445 "2021-06-23T14:32:15Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [June 23, 2021, 2:32pm UTC](https://discourse.julialang.org/t/setting-number-of-threads-at-startup-for-vs-code/63445/1 "2021-06-23T14:32:15Z")

</div>

Hello,

I am trying to set the number of threads (such as 4) in Julia by using the command:  
JULIA\_NUM\_THREADS=4  
in CMD in window before launching Julia, which works.

However, I need to set this issue inside “.jl” file by typing the command:  
$ JULIA\_NUM\_THREADS=4 ./julia  
at the beginning but it seems not working when I check the threads number by “Threads.threadid()” in the opened REPL.  
By the way, I am using VS code to run the “.jl” file.

Thanks!  
Amro

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [June 23, 2021, 2:33pm UTC](https://discourse.julialang.org/t/setting-number-of-threads-at-startup-for-vs-code/63445/2 "2021-06-23T14:33:05Z")

</div>

> [@JULIA\_NUM\_THREADS in VS Code (Windows 10 + WSL)](https://discourse.julialang.org/t/julia-num-threads-in-vs-code-windows-10-wsl/28794/6):
>
> You can find it now in the settings of the Julia extension of vscode. [grafik]

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [June 23, 2021, 2:54pm UTC](https://discourse.julialang.org/t/setting-number-of-threads-at-startup-for-vs-code/63445/3 "2021-06-23T14:54:56Z")

</div>

Thanks for the reply! I tried and it works.

However, can we define the number of threads from inside the “.jl” file rather set it manually?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [June 23, 2021, 3:09pm UTC](https://discourse.julialang.org/t/setting-number-of-threads-at-startup-for-vs-code/63445/4 "2021-06-23T15:09:09Z")

</div>

no, because it’s defined when the Julia process starts, which is before anything inside a `.jl` file is read. Same reason you can’t change number of threads on the fly

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [June 23, 2021, 3:20pm UTC](https://discourse.julialang.org/t/setting-number-of-threads-at-startup-for-vs-code/63445/5 "2021-06-23T15:20:33Z")

</div>

Thanks, I got it except the term “on the fly”.

Can we create a file (not .jl) which sets the number of threads and also calls my .jl file?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [June 23, 2021, 3:35pm UTC](https://discourse.julialang.org/t/setting-number-of-threads-at-startup-for-vs-code/63445/6 "2021-06-23T15:35:21Z")

</div>

> [@Amro](#):
>
> “on the fly”.

you open up a Julia REPL, `Threads.nthreads()` shows a number, there’s no way to change this value within the same session (“on the fly”, as this Julia process is running).

> [@Amro](#):
>
> we create a file (not .jl) which sets the number of threads and also calls my .jl file

yeah just use a bash or powershell steering script, or even a julia script 🙂

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [June 23, 2021, 3:40pm UTC](https://discourse.julialang.org/t/setting-number-of-threads-at-startup-for-vs-code/63445/7 "2021-06-23T15:40:47Z")

</div>

yeah just use a bash or powershell steering script, or even a julia script 🙂  
[/quote]  
Can you please give me an example using the julia script?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [June 23, 2021, 3:46pm UTC](https://discourse.julialang.org/t/setting-number-of-threads-at-startup-for-vs-code/63445/8 "2021-06-23T15:46:07Z")

</div>

well this is super backwards because you could have just called Julia on your original script… but here we go:

```julia
akako@ThinkX /tmp> cat t.jl
run(`julia -t $(ARGS[1]) myscript.jl`)
akako@ThinkX /tmp> cat myscript.jl 
println(Threads.nthreads())
akako@ThinkX /tmp> julia t.jl 1
1
akako@ThinkX /tmp> julia t.jl 3
3

```

by any chance you are just looking for the `-t`? [Multi-Threading · The Julia Language](https://docs.julialang.org/en/v1/manual/multi-threading/)

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [June 23, 2021, 4:06pm UTC](https://discourse.julialang.org/t/setting-number-of-threads-at-startup-for-vs-code/63445/9 "2021-06-23T16:06:50Z")

</div>

I am not sure if I made it right.  
1- I opened VS code  
2- I created a new .jl file and I put in it  
akako@ThinkX /tmp\> cat t.jl

run(`julia -t $(ARGS[5]) Test1.jl`)

akako@ThinkX /tmp\> cat Test1.jl

println(Threads.nthreads())  
akako@ThinkX /tmp\> julia t.jl 5

(Test1 is my .jl file and the number of the threads is 5).  
3- Then click “Julia: Excute file in REPL”

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [June 23, 2021, 4:13pm UTC](https://discourse.julialang.org/t/setting-number-of-threads-at-startup-for-vs-code/63445/10 "2021-06-23T16:13:37Z")

</div>

what’s your end goal

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [June 23, 2021, 4:18pm UTC](https://discourse.julialang.org/t/setting-number-of-threads-at-startup-for-vs-code/63445/11 "2021-06-23T16:18:00Z")

</div>

Create a file that (1- defines the number of threads; 2- and executes some calculations in julia script).  
I need to open this file by VScode and run it

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [June 23, 2021, 4:55pm UTC](https://discourse.julialang.org/t/setting-number-of-threads-at-startup-for-vs-code/63445/12 "2021-06-23T16:55:47Z")

</div>

> [@Amro](#):
>
> I need to open this file by VScode and run it

what’s wrong with using VSCode’s terminal to run `julia -t <num> yourscriipt.jl`?

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [June 23, 2021, 5:08pm UTC](https://discourse.julialang.org/t/setting-number-of-threads-at-startup-for-vs-code/63445/13 "2021-06-23T17:08:13Z")

</div>

I just want to make it more automatic, rather than typing the above line.  
However, while I typing the above line I got the following error"

 ![2](https://global.discourse-cdn.com/julialang/original/3X/e/7/e75c35897f72135b6033ae348c25137b59944822.png)
