sciter-sdk / pysciter

Python bindings for Sciter
https://sciter.com
MIT License
396 stars 40 forks source link

Support asynchronous script handler execution #70

Closed pravic closed 2 years ago

pravic commented 2 years ago

@script(threading=True) calls the handler in a separate thread using concurrent.futures.ThreadPoolExecutor.

Usage in TIScript:

// async execution, do not wait for result
view.func([1, 2]);

// async execution, wait for result
var r = await promise(function (resolve, reject) {
  view.func([1, 2], resolve, reject);
});

Usage in JS:

// do not wait for result
Window.this.xcall("func", [1, 2]);

// wait for result
let r = await new Promise(function (resolve, reject) {
  Window.this.xcall("func", [1, 2], resolve, reject);
});

Usage in Python:

@script(threading=True)
def func(self, a: int, b: int):
  return a + b
pravic commented 2 years ago

cc @AlecHaring

AlecHaring commented 2 years ago

Looks really good. I like your idea of using ThreadPoolExecutor so that you can make use of its submit method instead of manually creating new threads for each call. I suspect it would be significantly more efficient this way.

pravic commented 2 years ago

I suspect it would be significantly more efficient this way.

Well, definitely. Because it maintains a pool of threads: https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.ThreadPoolExecutor