Question: I want to write a python script that adds env variables to PATH. This script will be executed from ssh to windows virtual machine. I tried 2 methods and no one works.

Method 1 :

```
os.system("setx /M PATH "'"%PATH%;path1;path2"'"")

```

Method 2 :

```
os.environ["PATH"] += os.pathsep + os.pathsep.join(["path1"])

```

Could you help me please ?

Thanks in advance.

Answer: 
> 
> can take 3 to 4 seconds to start
> 
> 
> 

is caused by the threadpool. When all threads are busy it only slowly (2/second) creates new threads.

You could up the min amount of threads in the pool but especially for a web app you should research, test and measure that extensively. ASP.NET also is a big stakeholder in the threadpool.
`BeginInvoke` method dispatches actual work to a thread pool, as it's written in this [article](https://learn.microsoft.com/en-us/dotnet/standard/asynchronous-programming-patterns/asynchronous-programming-using-delegates). It may take some time actually, when there are no available idle threads. Thread pool may decide to wait for some work items completion or to add additional threads, accounting min and max limits. 

Some additional info may be found here [The managed threadpool](https://learn.microsoft.com/en-us/dotnet/standard/threading/the-managed-thread-pool) and there [Simple description of worker and IO threads in net](https://stackoverflow.com/questions/2099947/simple-description-of-worker-and-i-o-threads-in-net) and at remarks section of this article as well [ThreadPool.SetMinThreads](https://learn.microsoft.com/en-us/dotnet/api/system.threading.threadpool.setminthreads?view=netframework-4.7.2).

You should be aware that the same thread pool is used for HTTP requests processing, therefore it's usually senseless to offload custom non IO-bound work to the thread pool in web apps, as it won't give you any benefits and may even hurt performance due to additional thread switches. While `BeginInvoke` doesn't look as an invocation of asynchronous IO operation. 

It doesn't actually matter which concrete thread executes the work — client still have to wait for the response the same amount of time. Looks, like you may, probably, win some time by performing work in parallel, but it won't be possible under load as far as there won't be available threads at thread pool for processing both HTTP requests and your custom work items.

You may want to check this [thread](https://stackoverflow.com/questions/33764366/is-task-run-considered-bad-practice-in-an-asp-net-mvc-web-application) for some additional details on this theme. It's related to `Task`, but this doesn't matter as far as both `BeginInvoke` and `Task.Run` are using the same thread pool under the hood.