Posts

Showing posts with the label concurrency

How do you subscribe to state changes across threads?

How do you subscribe to state changes across threads? How do you conditionally wrap an IO action in a given thread depending on some mutable state stored in an MVar? My overall aim is to enable timeouts so that in a given game if a player does not send an action to the server within 30 seconds then the socket msg callback will be called with a special Timeout action to denote failure of the player to act. I am looking for a method to enable each socket thread to subscribe to a change in game state contained in an MVar. My current draft implementation for creating a Timeout action is as follows: -- This function processes msgs from authenticated clients authenticatedMsgLoop :: (MsgIn -> ReaderT MsgHandlerConfig (ExceptT Err IO) ()) -> MsgHandlerConfig -> IO () authenticatedMsgLoop msgCallback msgHandlerConfig@MsgHandlerConfig {..} = do finally (forever $ do maybeMsg <- timeout 1000000 (WS.receiveData clientConn) let parsedMsg = maybe (J...

Submit new task to executor after worker finishes

Submit new task to executor after worker finishes I am working on a web crawler that visits a page and extracts the link to look for a specific domain, if it does not find it it views the extracted links and repeats until it hits a page limit or finds the page. I find myself struggling to come up with sound logic to have the bot continue to queue tasks after it extracts the links because the tasks are being completed quickly and not enough time is given to submit the newly extracted links. How could I go about implementing that the crawler wait until it has no more links before shutting down the executor? I have included a basic overview of my multi threading implementation. I set the max threads to 3, and submit example.com 10 times (Seed domains) Spawn Thread visits the site and extracts the links then returns them to a string. My issue is that I need to be able to take those results and then put them into the queue. But the queue has already finished by that time. Any suggestions? U...