in reply to Re: Net::Async::WebSocket::Server in a separate main loop
in thread Net::Async::WebSocket::Server in a separate main loop

That is exactly the problem, I cannot use "loop->run" as this blocks the complete application. I need to find a way to call the websocket server logic from within my main loop only using "loop_once", as this is not a blocking call.

But it seems I have figured out what the problem is... The check for data on socket seems to "destroy" the async loop logic. I will need to find another way to get the main loop to hook into my logic so I can process websocket messages.

  • Comment on Re^2: Net::Async::WebSocket::Server in a separate main loop

Replies are listed 'Best First'.
Re^3: Net::Async::WebSocket::Server in a separate main loop
by NERDVANA (Priest) on Jan 12, 2023 at 22:52 UTC

    The main loop should not be checking to see if the socket is ready to read because that's the job of the event system. In your hybrid design, the main loop should just ask the event system every 1ms or so whether it has anything to do.

    But, what is your main loop doing that can't be done in IO::Async?

    Since perl is single-threaded, if you want anything to be event-driven, it should all be event-driven. That means you really do want to use ->run, and find a way to take the "main loop" of your script and re-package it into event-style code.

    Most event systems have an "idle" event that fires whenever the application lacks events to process, and you can use that to continue doing segments of a larger bulk task. But, you have to keep the segments of that large task brief, or it will block the events. For instance, if you want your application to respond to a network message within 1ms, then the 'idle' event should not run for more than 1ms at a time.