anki-code / xontrib-pipeliner

Let your pipe lines flow thru the Python code in xonsh.
BSD 2-Clause "Simplified" License
56 stars 5 forks source link

OSX: ppl — NameError: name '__xonsh__' is not defined #4

Closed MaximSavin closed 3 years ago

MaximSavin commented 3 years ago

I'm trying to use ppl with this pipe find . | ppl "str(num) + ' ' + line" and get error.

any idea how to fix it?

~/D/C/book $ XONSH_SHOW_TRACEBACK = True
~/D/C/book $ find . | ppl "str(num) + ' ' + line"
xonsh: To log full traceback to a file set: $XONSH_TRACEBACK_LOGFILE = <filename>
multiprocessing.pool.RemoteTraceback:
"""
Traceback (most recent call last):
  File "/usr/local/Cellar/python@3.9/3.9.2_4/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/pool.py", line 125, in worker
    result = (True, func(*args, **kwds))
  File "/usr/local/Cellar/xonsh/0.9.27/libexec/lib/python3.9/site-packages/xontrib/pipeliner_parallel.py", line 28, in __call__
    return self.f(x)
  File "/usr/local/Cellar/xonsh/0.9.27/libexec/lib/python3.9/site-packages/xontrib/pipeliner_parallel.py", line 10, in f
    ctx = __xonsh__.ctx
NameError: name '__xonsh__' is not defined
"""

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/Cellar/xonsh/0.9.27/libexec/lib/python3.9/site-packages/xonsh/procs/proxies.py", line 508, in run
    r = self.f(self.args, sp_stdin, sp_stdout, sp_stderr, spec, spec.stack)
  File "/usr/local/Cellar/xonsh/0.9.27/libexec/lib/python3.9/site-packages/xonsh/procs/proxies.py", line 305, in proxy_three
    return f(args, stdin, stdout)
  File "/usr/local/Cellar/xonsh/0.9.27/libexec/lib/python3.9/site-packages/xontrib/pipeliner.py", line 65, in _ppl
    xppl.go(func_args, stdout)
  File "/usr/local/Cellar/xonsh/0.9.27/libexec/lib/python3.9/site-packages/xontrib/pipeliner_parallel.py", line 23, in go
    for result in parallel_tasks:
  File "/usr/local/Cellar/python@3.9/3.9.2_4/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/pool.py", line 870, in next
    raise value
NameError: name '__xonsh__' is not defined

Maybe have to import builtins?

import builtins
anki-code commented 3 years ago

Hi! Thank you for reporting! The ppl functionality is experimental. I can repeat this on Mac and can't repeat this on Linux with Python 3.8 on both OS. Looks like the threading works different on Mac and Linux. Sad but the builtins will not help. If you can debug the threading on Mac it will be great. I will take a look into this when I have more time. Please star the repo if you like the idea of pipeliner. Thanks!

anki-code commented 3 years ago

On MacOS global variables are not accessible from child processes (multiprocessing.Pool):

on Python 3.8 mac multiprocessing switched to spawn you have to use https://docs.python.org/3/library/multiprocessing.shared_memory.html to share content between processes or pass it to be pickled in the args of imap_unordered

anki-code commented 3 years ago

I've fixed the error and made release xontrib-pipeliner 0.3.3 but there are known issues around multicore pipelining on Mac that described in the "Known issues" section in the README now. Thanks for reporting this!