Skip to content

Commit

Permalink
fixes #550
Browse files Browse the repository at this point in the history
  • Loading branch information
jph00 committed May 11, 2024
1 parent 24a10e8 commit 8cdc7a2
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 16 deletions.
1 change: 1 addition & 0 deletions fastcore/all.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
from .meta import *
from .imports import *
from .script import *
from .xml import *
22 changes: 14 additions & 8 deletions fastcore/parallel.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,20 @@
except: pass

# %% ../nbs/03a_parallel.ipynb 4
def threaded(f):
"Run `f` in a thread, and returns the thread"
@wraps(f)
def _f(*args, **kwargs):
res = Thread(target=f, args=args, kwargs=kwargs)
res.start()
return res
return _f
def threaded(process=False):
"Run `f` in a `Thread` (or `Process` if `process=True`), and returns it"
def _r(f):
@wraps(f)
def _f(*args, **kwargs):
res = (Thread,Process)[process](target=f, args=args, kwargs=kwargs)
res.start()
return res
return _f
if callable(process):
o = process
process = False
return _r(o)
return _r

# %% ../nbs/03a_parallel.ipynb 6
def startthread(f):
Expand Down
1 change: 1 addition & 0 deletions fastcore/xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def xt(tag:str, *c, **kw):
# %% ../nbs/11_xml.ipynb 9
def to_xml(elm, lvl=0):
"Convert `xt` element tree into an XML string"
if hasattr(elm, '__xt__'): elm = elm.__xt__()
sp = ' ' * lvl
if not isinstance(elm, list):
if isinstance(elm, str): elm = escape(elm)
Expand Down
22 changes: 14 additions & 8 deletions nbs/03a_parallel.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,20 @@
"outputs": [],
"source": [
"#|export\n",
"def threaded(f):\n",
" \"Run `f` in a thread, and returns the thread\"\n",
" @wraps(f)\n",
" def _f(*args, **kwargs):\n",
" res = Thread(target=f, args=args, kwargs=kwargs)\n",
" res.start()\n",
" return res\n",
" return _f"
"def threaded(process=False):\n",
" \"Run `f` in a `Thread` (or `Process` if `process=True`), and returns it\"\n",
" def _r(f):\n",
" @wraps(f)\n",
" def _f(*args, **kwargs):\n",
" res = (Thread,Process)[process](target=f, args=args, kwargs=kwargs)\n",
" res.start()\n",
" return res\n",
" return _f\n",
" if callable(process):\n",
" o = process\n",
" process = False\n",
" return _r(o)\n",
" return _r"
]
},
{
Expand Down
1 change: 1 addition & 0 deletions nbs/11_xml.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@
"#| export\n",
"def to_xml(elm, lvl=0):\n",
" \"Convert `xt` element tree into an XML string\"\n",
" if hasattr(elm, '__xt__'): elm = elm.__xt__()\n",
" sp = ' ' * lvl\n",
" if not isinstance(elm, list):\n",
" if isinstance(elm, str): elm = escape(elm)\n",
Expand Down

0 comments on commit 8cdc7a2

Please sign in to comment.