Hello,
I am writing here bacause I am trying to know how threads work with Sublime Text. I am trying to write plugin which connects to the external service and I need to have some kind of listener working in the thread as long as user wants to be connected.
Just imagine the situation. We have an Python module with only one class. In this class we can find some methods. The most important ones are: connect, send_data and disconnect. We are creating only one instance of given class. Nextly we run connect method (if handshake ends successfully, listener starts in the background). In the meantime we can use send_data method as much as we want and after every send_data method call there will be something to read by listener. User can disconnect from the service at any time. This is the scenerio I want to reach.
For now I have problem with more, more easily topic:
import sublime
import sublime_plugin
import threading
class Background(threading.Thread):
__instance = None
@staticmethod
def get_instance():
if Background.__instance == None:
Background()
return Background.__instance
def __init__(self):
if Background.__instance != None:
raise Exception("Cannot create next instance of this class (singleton).")
else:
Background.__instance = self
self.counter = 1
threading.Thread.__init__(self)
def run(self):
while self.counter <= 10000:
print(str(self.counter))
self.counter = self.counter + 1
class TestCommand(sublime_plugin.WindowCommand):
def run(self):
bg = Background.get_instance()
bg.start()
This piece of code makes Sublime Text 3 UI freeze. How should be it written correctly to avoid freezing?
Cheers