Closed TheOnlyArtz closed 5 years ago
Hi,
Of course sleep() will block. I don’t think sleep() is asynchronous in any language.
To do this sort of thing, you either need to use a dedicated thread (gives you better precision of the timing but is more complex) or use call_out() (simpler but no guarantees the callback will be called on the exact second delay you specified).
/Marcus
On 2 Dec 2018, at 19:05, Amit Katz notifications@github.com wrote:
Hey, I got some blocking code which I want to run asynchronously
Let's say I got this function
/*
- Make the client to start heartbeating
@param {int} time - The time for the interval to repeat */ void heartbeat(int time) { sleep(time/1000);
mapping mappingPayload = ([ "op": 1, "d": sequence ]);
// Send heartbeat payload string payload = Standards.JSON.encode(mappingPayload); ws->send_text(payload);
write("Heartbeat payload has been sent successfully\n"); heartbeat(time); }```
sleep(time/1000);
blocks the whole code, not just the function, how can I runheartbeat
asynchronously — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/pikelang/Pike/issues/15, or mute the thread https://github.com/notifications/unsubscribe-auth/ABHYK8nZyN8FtWkF72iDqrSOkBGyKLtyks5u1BZ3gaJpZM4Y9hUt.
Hi, Of course sleep() will block. I don’t think sleep() is asynchronous in any language. To do this sort of thing, you either need to use a dedicated thread (gives you better precision of the timing but is more complex) or use call_out() (simpler but no guarantees the callback will be called on the exact second delay you specified). /Marcus … On 2 Dec 2018, at 19:05, Amit Katz @.**> wrote: Hey, I got some blocking code which I want to run asynchronously Let's say I got this function / Make the client to start heartbeating @param {int} time - The time for the interval to repeat */ void heartbeat(int time) { sleep(time/1000); mapping mappingPayload = ([ "op": 1, "d": sequence ]); // Send heartbeat payload string payload = Standards.JSON.encode(mappingPayload); ws->send_text(payload); write("Heartbeat payload has been sent successfully\n"); heartbeat(time); }
``
sleep(time/1000);blocks the whole code, not just the function, how can I run
heartbeat` asynchronously — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub <#15>, or mute the thread https://github.com/notifications/unsubscribe-auth/ABHYK8nZyN8FtWkF72iDqrSOkBGyKLtyks5u1BZ3gaJpZM4Y9hUt.
Thank you for the response, I was trying to use call_out()
but I couldn't make it to repeat so it doesn't fill my needs, as I said in #12 at the last comment I left.
If you want it to repeat, you have to set up a new call_out() at the end of the heartbeat() method.
On 2 Dec 2018, at 19:44, Amit Katz notifications@github.com wrote:
Hi, Of course sleep() will block. I don’t think sleep() is asynchronous in any language. To do this sort of thing, you either need to use a dedicated thread (gives you better precision of the timing but is more complex) or use call_out() (simpler but no guarantees the callback will be called on the exact second delay you specified). /Marcus … <x-msg://7/#> On 2 Dec 2018, at 19:05, Amit Katz @.*> wrote: Hey, I got some blocking code which I want to run asynchronously Let's say I got this function / Make the client to start heartbeating @param https://github.com/param {int} time - The time for the interval to repeat / void heartbeat(int time) { sleep(time/1000); mapping mappingPayload = ([ "op": 1, "d": sequence ]); // Send heartbeat payload string payload = Standards.JSON.encode(mappingPayload); ws->send_text(payload); write("Heartbeat payload has been sent successfully\n"); heartbeat(time); }
`` sleep(time/1000); blocks the whole code, not just the function, how can I run
heartbeat` asynchronously — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub <#15 https://github.com/pikelang/Pike/issues/15>, or mute the thread https://github.com/notifications/unsubscribe-auth/ABHYK8nZyN8FtWkF72iDqrSOkBGyKLtyks5u1BZ3gaJpZM4Y9hUt https://github.com/notifications/unsubscribe-auth/ABHYK8nZyN8FtWkF72iDqrSOkBGyKLtyks5u1BZ3gaJpZM4Y9hUt.Thank you for the response, I was trying to use call_out() but I couldn't make it to repeat so it doesn't fill my needs, as I said in #12 https://github.com/pikelang/Pike/issues/12 at the last comment I left.
— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/pikelang/Pike/issues/15#issuecomment-443531780, or mute the thread https://github.com/notifications/unsubscribe-auth/ABHYK4xTk5y7LodcHLzU3haeZ998b_twks5u1B-QgaJpZM4Y9hUt.
FYI: Pike 9.0 has asynchronous functions, and calling sleep()
in such functions performs an asynchronous sleep (using call_out()
).
Hey, I got some blocking code which I want to run asynchronously
Let's say I got this function
sleep(time/1000);
blocks the whole code, not just the function, how can I runheartbeat
asynchronously