Sublime Forum

Multithreaded plugin

#1

Hello guys,

Since few days I try to write my ST3 plugin to sending XML in background and view received data in new file.

I created transaction object to handle creating, sending and receiving data:

class Trx(threading.Thread):
    def __init__(self, address, request, edit):
        self.address = address  # (host, port)
        self.request = request
        self.response = None
        self._edit = edit
        threading.Thread.__init__(self)
        self.process();

def run(self):
        new_view = sublime.active_window().new_file()
        new_view.insert(self._edit, 0, self.response)
        new_view.set_syntax_file('Packages/XML/XML.tmLanguage')

class RequestSenderCommand(sublime_plugin.TextCommand):

    def run(self, edit, address=None):

        if address is None:
            sublime.error_message('No address')
            return

        window = sublime.active_window()

        region = sublime.Region(0, self.view.size())
        request = self.view.substr(region)
        host, port = address.split(':')
        sublime.status_message('Sending...')
        thread = Trx((host,port), request, edit)
        thread.start()

        ThreadProgress(thread, 'Sending', 'Sent')

But there is a problem with edit object. It cannot be passed to other object and I don’t know how to view response.

Any suggestions?

0 Likes

#2

Use commands to manipulate the view, such as:

new_view.run_command('insert', {'characters':'text to insert'})
1 Like

#3

That, or use edit.py, which is an edit abstraction for both ST2 and ST3. It effectively does the same (uses sublime.run_command) but masked and allows for more flexible interaction with the view.

1 Like