PetterS / quickjs

Thin Python wrapper of https://bellard.org/quickjs/
https://github.com/bellard/QuickJS
MIT License
177 stars 19 forks source link

Fix #62: Implement async job execution #65

Closed qwenger closed 2 years ago

qwenger commented 2 years ago

Fix #62.

Note that errors occurring in asynchronous code do not seem to be raised (even though the case JS_ExecutePendingJob(...) < 0 is handled). But this seems to be the case in the native qjs as well. EDIT: this seems to be a matter of using JS_SetHostPromiseRejectionTracker, see https://github.com/bellard/quickjs/issues/87. Should we use it, or maybe conditionally with a flag on the context (the question also applies to #64 ...)?

PetterS commented 2 years ago

Hey this looks great! Let me see if I can find the time to review this properly this weekend. Thanks!

qwenger commented 2 years ago

Note: with this patch, in the presence of potentially asynchronous code, the standard thing would be to call execute_pending_job repeatedly:

ctx.eval("/* some job launching */")
while ctx.execute_pending_job():
    pass
ctx.eval("/* some more code */")

If this back-and-forth between Python and JS is too slow, we could implement an execute_all_pending_jobs method with a loop on the C side, just like js_std_loop (https://github.com/bellard/quickjs/blob/master/quickjs-libc.c#L3878).