Sublime Forum

Concurrency practice for handling plugin I/O requests

#1

Hey all, I’m trying to develop a plugin for Sublime which involves calling an API and handling the response. Currently it works really well, however sometimes the operation take a bit longer (though not noticeable longer) and it triggers the cursor to go into spinning wheel mode. I want to know what is the common practice for plugins to speed up requests sent to APIs? Is it threading? Asynchronous? Multiprocessing? Appreciate any input!

0 Likes

#2

I used threading and sublime.set_timeout_async().

sublime.set_timeout_async() is handy but it actually blocks another ST’s (non-UI) thread so it’s not suitable for a task which may take a long time. Not sure how asyncio may help since I’ve never seen people use it yet.

0 Likes

#3

I’m still learning, so if it’s not too much to ask can you give me some examples of how you handle it? Appreciate your response!

0 Likes

#4
import threading


def my_function(arg1, arg2) -> None:
    print(arg1, arg2)


t = threading.Thread(target=my_function, args=('foo', 'bar'))
t.start()

# -----------------------------------------------------------

import sublime

sublime.set_timeout_async(lambda: my_function('foo', 'bar'))

All functions scheduled by sublime.set_timeout_async() will run in (the same) another thread synchronously (i.e., they block each other).

1 Like

#5

This might help as well How to access thread results

1 Like

#6

this is great! Thank you so much!

0 Likes