Skip to content

Commit

Permalink
updated map such that context vars are propagated through to the inne…
Browse files Browse the repository at this point in the history
…r threads created.
  • Loading branch information
djl11 committed Oct 31, 2024
1 parent 74f1ad1 commit 9c8ddaa
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
Empty file added notebook.py
Empty file.
5 changes: 5 additions & 0 deletions unify/utils/map.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
import threading
import contextvars
from typing import Any, List


Expand Down Expand Up @@ -27,6 +28,9 @@ def map(fn: callable, *args, mode="threading", **kwargs) -> Any:
if mode == "threading":

def fn_w_indexing(rets: List[None], thread_idx: int, *a, **kw):
for var, value in kw["context"].items():
var.set(value)
del kw["context"]
ret = fn(*a, **kw)
rets[thread_idx] = ret

Expand All @@ -38,6 +42,7 @@ def fn_w_indexing(rets: List[None], thread_idx: int, *a, **kw):
k: v[i] if (isinstance(v, list) or isinstance(v, tuple)) else v
for k, v in kwargs.items()
}
kw["context"] = contextvars.copy_context()
thread = threading.Thread(
target=fn_w_indexing,
args=(returns, i, *a),
Expand Down

0 comments on commit 9c8ddaa

Please sign in to comment.