Sublime Forum

Is there alternative for time.sleep() method to delay some functionality

#1

I found that if we use time.sleep() in plugin this leads to editor stop responding. How to implement delay functionality without affecting sublime workflow?

0 Likes

#2

I guess you can use the sublime.set_timeout or sublime.set_timeout_async depending on your needs. Both functions are thread safe and doesn’t block the main UI. My guess is time.sleep() is a blocking call that interferes with the main thread, hence the editor stops responding.

2 Likes

#3

You’re correct. time.sleep() suspends the current thread for a certain amount of time, if done within a synchronous callback this will also suspend the main application.

0 Likes

#4

Can you provide a simple example for sublime.set_timeout_async usage

0 Likes

#5

The function just takes a callback function & a delay, which indicates the time after which the callback must be executed. You can find the API reference here :- https://www.sublimetext.com/docs/3/api_reference.html#sublime

0 Likes

#6

I was unable to use set_timeout_async(callback, delay)
I wasn’t adding delay to function like time.sleep() does.

0 Likes

#7

Can you please tell what are you trying to accomplish with set_timeout_async and what have you tried on your end ? time.sleep() will halt the code execution for the specified time. The closest to something similar to time.sleep() in the API are the 2 methods referred above.

0 Likes