Sublime Forum

How Python threading is internally organized by SublimeText?

#1

Hi Guys!

I came across couple of plugin tutorials and I feel confused about correct use of Python threads in SublimeText plugin development.

It is the common issue with Python GIL which doesn’t allow two threads to do calculations at the same time (at least with CPython interpreter, which I guess is used inside Sublime?..). E.g. you can have N threads making IO (say, downloading pages) at a time, but only one thread actually doing some CPU extensive work, like parsing the page’s content.

So, what is the sense in running CPU extensive operations in separate thread in plugin (like walking through project directories and examining files for further autocompletion) if it still will make the whole interpreter wait for it?

How is it internally organized in SublimeText and how thus to use threads correctly?

Thank you in advance,
Ilya.

0 Likes

#2

[quote=“itzhak”]So, what is the sense in running CPU extensive operations in separate thread in plugin (like walking through project directories and examining files for further autocompletion) if it still will make the whole interpreter wait for it?
[/quote]

It’s true that the GIL only allows one thread to run the interpreter at any time, however there’s still scheduling going on so one thread’s work is interrupted and the interpreter starts interpreting another on a regular basis. Say your task takes 10 seconds to complete this means that if you do it in the main thread, the mainthread will be blocked for 10 seconds and if you do it in a separate thread, the interpreter will switch between the two threads many, many times per second ensuring that both threads get work done.

0 Likes