0

I was wondering if it is possible to run threads each with its own environment variables.

I am currently using environment variables to set my proxies like this:

os.environ["HTTP_PROXY"] = proxy["http"]
os.environ["HTTPS_PROXY"] = proxy["https"]

But I noticed that each thread will just use the proxy that has last been set. It is not possible for me to use the requests library for this purpose.

Is there a way that I can achieve this without using requests.Session() for each thread?

mtedu
  • 41
  • 7
  • This might be what you're looking for: https://stackoverflow.com/questions/38348556/how-to-set-a-thread-specific-environment-variable-in-python – alxgmpr Nov 06 '21 at 18:49
  • Out of curiosity, why? Environment variables were never meant as a means of communication between threads. They are meant as a way for something outside the program to pass in configuration info. If you are using a library that forces you to use the environment as the only means of passing in parameters, then proceed with caution! That might be a sign that the library is not thread-safe. – Solomon Slow Nov 06 '21 at 23:00

1 Answers1

2

I was wondering if it is possible to run threads each with its own environment variables.

No. Env vars live in the address space of the process and thus are global to all threads of the process.

Kurtis Rader
  • 6,734
  • 13
  • 20