-
Notifications
You must be signed in to change notification settings - Fork 227
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
peg-grammar is not available in threads #1180
Comments
Yes, although not reported in an issue AFAIK, this has been noted in discussions at one or more of the gitter / matrix channels. A work-around I'd been using was putting: (setdyn :peg-grammar default-peg-grammar) in the definition of a function passed to It seems to me that either it should be documented behavior (along with any other caveats [1] when spawning a thread) or the current behavior should be adjusted. [1] I noticed that |
I must admit I have not looked deeply at the memory model for threads so I'm not sure what the original design decisions are, but I think conceptually it might make sense to have new threads inherit the parent's dynamic bindings? |
For So if I do: (ev/thread (coro (pp (peg/match {:main :s*} "")))) I get:
Currently (ev/spawn-thread :i (peg/match {:main :s*} "")) I don't suppose that would break any code -- an initial keyword in a function body presumably doesn't do much so I doubt many people have been using them for anything "usual" [1]. [1] I've seen use as what looks like labeling (presumably for human readers) as well as metadata that gets parsed by other code. |
So the
Yes, eval needs to have available function definitions in it's environment, so in order to see all available functions they would need to all be copied to the new thread. For efficiency, the default behavior is to copy nothing that isn't needed. |
For completeness, as a work around I should mention that For example (defn wrap-closure-for-thread
"Wrap a closure in a fiber that captures the current dynamic bindings state."
[f]
(def env @{})
(each k (all-dynamics) (put env k (dyn k)))
(fiber/setenv (fiber/new f :t) env))
# Use with ev/thread
(ev/thread (wrap-closure-for-thread (fn [&] (pp (dyn *peg-grammar*))))) Edit: @sogaiu already mentioned this but above is more explicit about what is happening and how to capture precisely the state that you want. |
This snippet:
results in the following error:
Root cause is that the
*peg-grammar*
dynamic binding does not make it into the thread, just like any other dynamic binding.The text was updated successfully, but these errors were encountered: