So I’ve been playing around with building some tools for some work I’ve been doing, and I’m a bit stuck. Should I be handling my own threads, or should I be using set_timeout_async or?
The plugin I’m writing is conceptually simple. Essentially, I’m writing a command that accepts a regex pattern as input and then uses this to filter individual lines in a (big) prepared text file. I would like the lines that pass this filter to be appended to a file that is created when the command fires.
This is all more or less working, but I’m hanging the main thread quite a lot. I’ve tried a variety of implementations and I’m at a point where I thought I’d look for feedback.
my current solution is basically to have a subclass of threading.Thread() that inits with the specified pattern, and then appends passes to a list property. this thread gets passed to a run_loop() function, which looks like this:
[code] def run_loop(self, thread, lock):
lock.acquire()
if len(thread.results):
for line in thread.results:
self._output_view.run_command(‘print_filtered_line’, {‘line’: line})
self.view.set_status('filtering lines', '%d of %d' % \
(thread.good_lines, thread.seen_lines))
thread.results = list()
lock.release()
if thread.is_alive():
print('thread still alive')
sublime.set_timeout_async(lambda: self.run_loop(thread, lock), 50)
else:
print('finished')
[/code]
which in turn passes each output string to this guy:
[code]last_cursor_position = 0
class PrintFilteredLineCommand(sublime_plugin.TextCommand):
def run(self, edit, line):
global last_cursor_position
self.view.insert(edit, last_cursor_position, line)
last_cursor_position = self.view.full_line(last_cursor_position).end()
[/code]
I can see already a lot of ways that I might go about improving this (i.e. just keeping a string buffer instead of a list, and appending all new output at once) but this is all just sort of hunch-based, for me, and I thought I would see if anybody had any slightly more concrete suggestions as to how I might do what I’m trying to do, here.