skeeto / emacs-aio

async/await for Emacs Lisp
The Unlicense
218 stars 12 forks source link

how to set timeouts with aio #28

Open mooseyboots opened 2 years ago

mooseyboots commented 2 years ago

i just gave this a library a go for a simple url-retrieve functionality.

i'm wondering how to set a timeout for it?

so far i just used code as in your example:

((let ((response (aio-await (aio-url-retrieve url))))
  do stuff w/ (cdr response)

i see there's aio-timeout and aio-idle but it's unclear to me how to use these to give up on such a request, if that's even what they can be used for? (since using aio in my code, sometimes a response comes in about 10 minutes late, longer after i have given up and re-run the command to fetch the url.

alphapapa commented 1 year ago

Take a look at https://github.com/skeeto/emacs-aio/blob/da93523e235529fa97d6f251319d9e1d6fc24a41/aio-test.el#L17

mooseyboots commented 1 year ago

thanks for the pointer, i think i have something working with it.

(defmacro aio-with-timeout (timeout &rest body)
  "Run body asynchronously.

If TIMEOUT seconds passes without completion, signal an
aio-timeout error."
  (declare (indent 1))
  `(let* ((promises (list (aio-with-async ,@body)
                          (aio-timeout ,timeout)))
          (select (aio-make-select promises)))
     (aio-with-async
       (aio-await (aio-await (aio-select select))))))

it would be nice gracefully handle a timeout rather than just have the aio-timeout error , which to me appears as Error running timer ‘funcall’: (aio-timeout . 0). but thats for another day for me.