[{"data":1,"prerenderedAt":409},["ShallowReactive",2],{"doc:\u002Fguides\u002Fconcurrency":3,"nav:guides":361},{"doc":4,"source":360},{"id":5,"title":6,"body":7,"description":353,"extension":354,"meta":355,"navigation":152,"path":356,"seo":357,"stem":358,"__hash__":359},"guides\u002Fguides\u002Fconcurrency.md","Concurrency",{"type":8,"value":9,"toc":348},"minimark",[10,14,27,32,66,308,318,322,329,333,344],[11,12,6],"h1",{"id":13},"concurrency",[15,16,17,18,22,23,26],"p",{},"A ",[19,20,21],"code",{},"Manager"," owns one ",[19,24,25],{},"pkl server"," process and multiplexes every evaluator over it. Starting the server is the slowest step of an evaluation, so create one manager and share it.",[28,29,31],"h2",{"id":30},"what-is-thread-safe","What is thread-safe",[33,34,35,51,54,60],"ul",{},[36,37,38,39,42,43,46,47,50],"li",{},"Any thread may call ",[19,40,41],{},"createEvaluator",", ",[19,44,45],{},"evaluate",", and ",[19,48,49],{},"close"," on a shared manager at any time.",[36,52,53],{},"Any thread may evaluate through any evaluator, including several threads through the same evaluator at once. Requests carry ids and their responses are matched independently, so a slow evaluation does not hold up a fast one.",[36,55,56,59],{},[19,57,58],{},"Evaluator.close"," is idempotent. It waits for the evaluator's reader and logger callbacks, then tells the server.",[36,61,62,65],{},[19,63,64],{},"Manager.deinit"," must run once, after your threads stop starting requests. It cancels requests still pending and waits for callbacks to finish.",[67,68,73],"pre",{"className":69,"code":70,"language":71,"meta":72,"style":72},"language-zig shiki shiki-themes github-light github-dark","const shared = try manager.createEvaluator(zig_pkl.Options.restricted(), &rejected);\ndefer shared.close();\n\nvar workers: [4]std.Thread = undefined;\nfor (&workers, 0..) |*worker, index| {\n    worker.* = try std.Thread.spawn(.{}, work, .{ shared, index });\n}\nfor (workers) |worker| worker.join();\n","zig","",[19,74,75,132,147,154,189,228,274,280],{"__ignoreMap":72},[76,77,80,84,88,91,94,97,101,104,107,110,112,115,117,120,123,126,129],"span",{"class":78,"line":79},"line",1,[76,81,83],{"class":82},"szBVR","const",[76,85,87],{"class":86},"s4XuR"," shared",[76,89,90],{"class":82}," =",[76,92,93],{"class":82}," try",[76,95,96],{"class":86}," manager",[76,98,100],{"class":99},"sVt8B",".",[76,102,41],{"class":103},"sScJk",[76,105,106],{"class":99},"(",[76,108,109],{"class":86},"zig_pkl",[76,111,100],{"class":99},[76,113,114],{"class":86},"Options",[76,116,100],{"class":99},[76,118,119],{"class":103},"restricted",[76,121,122],{"class":99},"(), ",[76,124,125],{"class":82},"&",[76,127,128],{"class":86},"rejected",[76,130,131],{"class":99},");\n",[76,133,135,138,140,142,144],{"class":78,"line":134},2,[76,136,137],{"class":82},"defer",[76,139,87],{"class":86},[76,141,100],{"class":99},[76,143,49],{"class":103},[76,145,146],{"class":99},"();\n",[76,148,150],{"class":78,"line":149},3,[76,151,153],{"emptyLinePlaceholder":152},true,"\n",[76,155,157,160,163,166,170,173,176,178,181,183,186],{"class":78,"line":156},4,[76,158,159],{"class":82},"var",[76,161,162],{"class":86}," workers",[76,164,165],{"class":99},": [",[76,167,169],{"class":168},"sj4cs","4",[76,171,172],{"class":99},"]",[76,174,175],{"class":86},"std",[76,177,100],{"class":99},[76,179,180],{"class":86},"Thread",[76,182,90],{"class":82},[76,184,185],{"class":82}," undefined",[76,187,188],{"class":99},";\n",[76,190,192,195,198,200,203,205,208,211,214,217,219,222,225],{"class":78,"line":191},5,[76,193,194],{"class":82},"for",[76,196,197],{"class":99}," (",[76,199,125],{"class":82},[76,201,202],{"class":86},"workers",[76,204,42],{"class":99},[76,206,207],{"class":168},"0",[76,209,210],{"class":99},"..) ",[76,212,213],{"class":82},"|*",[76,215,216],{"class":86},"worker",[76,218,42],{"class":99},[76,220,221],{"class":86},"index",[76,223,224],{"class":82},"|",[76,226,227],{"class":99}," {\n",[76,229,231,234,236,239,241,243,246,248,250,252,255,258,261,264,267,269,271],{"class":78,"line":230},6,[76,232,233],{"class":86},"    worker",[76,235,100],{"class":99},[76,237,238],{"class":82},"*",[76,240,90],{"class":82},[76,242,93],{"class":82},[76,244,245],{"class":86}," std",[76,247,100],{"class":99},[76,249,180],{"class":86},[76,251,100],{"class":99},[76,253,254],{"class":103},"spawn",[76,256,257],{"class":99},"(.{}, ",[76,259,260],{"class":86},"work",[76,262,263],{"class":99},", .{ ",[76,265,266],{"class":86},"shared",[76,268,42],{"class":99},[76,270,221],{"class":86},[76,272,273],{"class":99}," });\n",[76,275,277],{"class":78,"line":276},7,[76,278,279],{"class":99},"}\n",[76,281,283,285,287,289,292,294,296,298,301,303,306],{"class":78,"line":282},8,[76,284,194],{"class":82},[76,286,197],{"class":99},[76,288,202],{"class":86},[76,290,291],{"class":99},") ",[76,293,224],{"class":82},[76,295,216],{"class":86},[76,297,224],{"class":82},[76,299,300],{"class":86}," worker",[76,302,100],{"class":99},[76,304,305],{"class":103},"join",[76,307,146],{"class":99},[15,309,310,311,100],{},"See ",[312,313,315],"a",{"href":314},"..\u002F..\u002Fexamples\u002Fconcurrency\u002Fmain.zig",[19,316,317],{},"examples\u002Fconcurrency",[28,319,321],{"id":320},"inside-the-manager","Inside the manager",[15,323,324,325,100],{},"One response thread reads frames from the server and wakes the request each one answers. Log messages are delivered on that thread. Reader callbacks go to a bounded pool of worker threads that start on demand; see ",[312,326,328],{"href":327},"readers#threads","Readers",[28,330,332],{"id":331},"failure","Failure",[15,334,335,336,339,340,100],{},"A broken pipe, a server exit, or a malformed frame is terminal for the manager. Every pending request fails with that error, later requests fail immediately with it, and ",[19,337,338],{},"Manager.failure()"," returns it. Create a new manager to recover. See ",[312,341,343],{"href":342},"errors","Errors",[345,346,347],"style",{},"html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .s4XuR, html code.shiki .s4XuR{--shiki-default:#E36209;--shiki-dark:#FFAB70}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":72,"searchDepth":134,"depth":134,"links":349},[350,351,352],{"id":30,"depth":134,"text":31},{"id":320,"depth":134,"text":321},{"id":331,"depth":134,"text":332},"A Manager owns one pkl server process and multiplexes every evaluator over it. Starting the server is the slowest step of an evaluation, so create one manager and share it.","md",{},"\u002Fguides\u002Fconcurrency",{"title":6,"description":353},"guides\u002Fconcurrency","WJj_BeBqxIXQr3zjNYXoBYI57b33-z_X8F52klHiln8","docs\u002Fguides\u002Fconcurrency.md",[362,366,367,370,374,378,382,386,390,394,398,402,405],{"title":363,"path":364,"stem":365},"Version compatibility","\u002Fguides\u002Fcompatibility","guides\u002Fcompatibility",{"title":6,"path":356,"stem":358},{"title":343,"path":368,"stem":369},"\u002Fguides\u002Ferrors","guides\u002Ferrors",{"title":371,"path":372,"stem":373},"Evaluation","\u002Fguides\u002Fevaluation","guides\u002Fevaluation",{"title":375,"path":376,"stem":377},"External readers","\u002Fguides\u002Fexternal-readers","guides\u002Fexternal-readers",{"title":379,"path":380,"stem":381},"Installation","\u002Fguides\u002Finstallation","guides\u002Finstallation",{"title":383,"path":384,"stem":385},"Logging","\u002Fguides\u002Flogging","guides\u002Flogging",{"title":387,"path":388,"stem":389},"Migrating from pkl-zig","\u002Fguides\u002Fmigration","guides\u002Fmigration",{"title":391,"path":392,"stem":393},"Ownership","\u002Fguides\u002Fownership","guides\u002Fownership",{"title":395,"path":396,"stem":397},"Projects","\u002Fguides\u002Fprojects","guides\u002Fprojects",{"title":399,"path":400,"stem":401},"Quick start","\u002Fguides\u002Fquickstart","guides\u002Fquickstart",{"title":328,"path":403,"stem":404},"\u002Fguides\u002Freaders","guides\u002Freaders",{"title":406,"path":407,"stem":408},"Security","\u002Fguides\u002Fsecurity","guides\u002Fsecurity",1792908673090]