5 ms·
sometimes you want multiple transactions within a single request and data will change between them Is this an issue with race conditions, or with alternating b
by kd0amg 15y ago
sometimes you want multiple transactions within a single request and data will change between them
Is this an issue with race conditions, or with alternating between reading and writing the state during a single request, or both, or something else entirely?
You may also take input from other sources than a database
This doesn't seem like it should make a difference: you write a function to get the necessary data from whatever source, then call that function in the page request handler (OK, yes, I/O is technically a side effect).
I agree that the processing you do with that data might require some significant rethinking, depending on how it was written/planned out before (sure, state-passing style is always there, though I don't enjoy doing it... really though, I've found that a lot of what I was used to doing imperatively is not all that awkward to do functionally).
Not to mention if you are using Comet or something where you hold a request open for a long time and push and pull allot of data some of which may not be persisted immediately.
What's the conventional way to do this in an imperative language?
- jiggy2011 15y agoIs this an issue with race conditions, or with alternating between reading and writing the state during a single request, or both, or something else entirely? Usually reading and then writing some state followed by waiting briefly to see if another thread will change the same value, if it does use then do something with the new value and return that to the request. I agree that the processing you do with that data might require some significant rethinking, depending on how it was written/planned out before (sure, state-passing style is always there, though I don't enjoy doing it... really though, I've found that a lot of what I was used to doing imperatively is not all that awkward to do functionally). Depends on how much more difficult it is , does it just require and adjustment of thinking to a new paradigm or is doing everything harder? I'm thinking of stuff like Haskell Monads here. I admit I have not much functional knowledge beyond some basic scheme so I'm curious about this. What's the conventional way to do this in an imperative language? I guess allocate stuff on the heap which other threads can modify, use monitors to control concurrent access.
- kd0amg 15y agodoes it just require and adjustment of thinking to a new paradigm or is doing everything harder? Depends on how restrictive your language is. Scheme makes it a lot of things easy, e.g. a sequence of statements is often easy to express with begin/let/let* , whereas Haskell is pickier about use of side effects. The biggest hurdle I had in learning scheme was getting used to using a recursive function where I would normally use a loop. (let* [(x (... DB query ...)) (y (... API query based on x ...))] (begin (... write some state based on x and y ...) (... wait a bit ...) (... read the same piece of state ...) (if (... it is unchanged ...) (... then ...) (... else ...)))) The line between imperative and functional starts to look a bit fuzzy, but let* and begin can be constructed from lambda expressions.