0

Update the title to clarify my question:

I was recently told that Python cannot truly thread and I find this confusing. Something to do with IO and CPU. Take for example I run multiple "Threads" to run multiple queries against an SQL Server all at the same time as to not take forever running them one after another. This is also done as to not block Tkinters mainloop as Tkinter is a single threaded library. At this point I feel like everything in python is single threaded...

So when bringing up this question I was told that Each thread can only occupy the CPU at once and the SQL server is handling the load. I understand that to a point but if this is the case then how is it that Tkinters mainloop is not blocked if the CPU can only work on one thread at a time then any time my other threads are doing stuff the mainloop should be blocked by this logic.

I know that cannot be true as the mainloop is running just fine and my GUI is not frozen so something else I am not understanding must be going on.

So my question is this:

How is this threading not blocking the tkinter mainloop if it is not parallel processing?

How exactly does Python "Thread" and what is the difference between IO and CPU in terms of threading?

This post has some information on my 2nd question. Still not what I want to know about the mainloop though.

Mike - SMT
  • 14,784
  • 4
  • 35
  • 79
  • Does this answer your question? [How can I use threading in Python?](https://stackoverflow.com/questions/2846653/how-can-i-use-threading-in-python) – imbr Jun 18 '20 at 18:21
  • Does this answer your question? [What is the global interpreter lock (GIL) in CPython?](https://stackoverflow.com/questions/1294382/what-is-the-global-interpreter-lock-gil-in-cpython) – Klaus D. Jun 18 '20 at 18:23
  • IIUC it boils down to the fact of how GIL is implemented in Python. It is generally recommended if your subtask is CPU intensive then you should use multiprocessing and not threading. But, if you have tasks which have I/O dependency then you can use multi threading and it is recommended – mad_ Jun 18 '20 at 18:23
  • Depends what you think "truly thread" means. Threads were invented something like a decade before multi-processor computer systems became a reality. They have other uses besides parallel computing. – Solomon Slow Jun 18 '20 at 18:25
  • @eusoubrasileiro I know how to use threading I just don't understand how it actually works. The mechanic of it all. – Mike - SMT Jun 18 '20 at 18:25
  • @mad_ Ok that makes since but can Python run anything in parallel even with mulitprocessing? The instructor sound liked this was not possible due to how Python was designed from the start. – Mike - SMT Jun 18 '20 at 18:26
  • @KlausD. That relates to CPython and I am not sure it is a complete answer for the base Python and the core of my question. – Mike - SMT Jun 18 '20 at 18:27
  • @eusoubrasileiro where that post is somewhat enlightening it still does not explain how the mainloop is not being blocked for Tkinter. Or at least I am not seeing the connection. – Mike - SMT Jun 18 '20 at 18:31
  • interpreting Python bytecode is limited to one concurrent thread per process, but things change when it hits native code. e.g. search for references to [`Py_BEGIN_ALLOW_THREADS`](https://docs.python.org/3/c-api/init.html#thread-state-and-the-global-interpreter-lock) in https://github.com/python/cpython/blob/3.8/Modules/_tkinter.c. the implementation of your database connector will be doing something similar at low levels – Sam Mason Jun 18 '20 at 18:45
  • 1
    CPython refers to the reference Python interpreter written in C. It is the most widely used interpterter and if you are running Python it is most likely that. – Klaus D. Jun 18 '20 at 19:03
  • @KlausD. Oh I see. I thought that Python, CPython, JPython and IronPython were all different but I didnt realize CPython is the base implementation of Python. The more you know :D I have been writing Python programs in a professional capacity for a few years now but I have no formal training so its all self taught. I sometimes mistake things like that. Thanks for the clarification. – Mike - SMT Jun 18 '20 at 19:22

0 Answers0