-1

I am using threading to run many instances of a while loop. It starts off fast, but it gets extremely slow, slower than it would be using a single thread. I am unable to provide code but I will when I can. Any help is appreciated.

  • 1
    Please add code related to your issue being addressed – shuberman Jul 10 '20 at 05:59
  • Welcome to [so]. Please be aware that the Q&A format is not suitable for open-ended questions where we *guess* what your problem is. Please [edit] your question to provide an [mcve] – even (and especially) if you cannot show your *actual* code, you should provide sample code that demonstrates the problem in isolation. See the [ask] page how to best help us help you. – MisterMiyagi Jul 10 '20 at 06:39

1 Answers1

1

As you have not added code yet, I can't help with the specific problem. With that being said, using a single thread is most of the time a better choice with Python ,with the exception of networking, where it might have the largest benefit (as well as other IO cases).

The main reason for that is that Python has a Global Interpreter Lock that prevents two threads in the same process from running Python code at the same time. While in other languages (like Java for example), multithreading can work across multiple processors and gain the most benefit. Additionally, multithreading has an overhead when run in Python, and may sometimes provide you with strange results due to race conditions or other errors that are harder to debug. As an alternative to multithreading you can attempt using multiprocessing, where that topic has been covered extensively in other stackoverflow question like: What are the differences between the threading and multiprocessing modules? .

alt-f4
  • 2,112
  • 17
  • 49