Skip to content
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

macOS: Unable to run yfinance-cache: python multiprocessing errors #67

Open
sonique6784 opened this issue Jul 6, 2024 · 6 comments
Open

Comments

@sonique6784
Copy link
Contributor

sonique6784 commented Jul 6, 2024

I'm not able to run yfinance-cache. I get the following python multiprocessing errors:

I tried with Python 3.12 and 3.9, macOS Ventura

What Python version do you recommend?

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/local/Cellar/python@3.9/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/spawn.py", line 116, in spawn_main
    exitcode = _main(fd, parent_sentinel)
  File "/usr/local/Cellar/python@3.9/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/spawn.py", line 125, in _main
    prepare(preparation_data)
  File "/usr/local/Cellar/python@3.9/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/spawn.py", line 236, in prepare
    _fixup_main_from_path(data['init_main_from_path'])
  File "/usr/local/Cellar/python@3.9/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/spawn.py", line 287, in _fixup_main_from_path
    main_content = runpy.run_path(main_path,
  File "/usr/local/Cellar/python@3.9/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 288, in run_path
    return _run_module_code(code, init_globals, run_name,
  File "/usr/local/Cellar/python@3.9/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 97, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "/usr/local/Cellar/python@3.9/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/path/to/venv/script.py", line 376, in <module>
    best_performing_asset(snp500)
  File "/path/to/venv/script.py", line 250, in best_performing_asset
    stock_data = get_asset_data(asset_list)
  File "/path/to/venv/script.py", line 219, in get_asset_data
    stock_data = yf.download(asset_list_joined, start=start, end=end, interval="1mo")["Close"]  # Download closing monthly.


  File "/path/to/venv/lib/python3.9/site-packages/yfinance_cache/yfc_multi.py", line 48, in download
    queue = multiprocessing.Manager().Queue()
  File "/usr/local/Cellar/python@3.9/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/context.py", line 57, in Manager
    m.start()
  File "/usr/local/Cellar/python@3.9/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/managers.py", line 554, in start
    self._process.start()
  File "/usr/local/Cellar/python@3.9/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/process.py", line 121, in start
    self._popen = self._Popen(self)
  File "/usr/local/Cellar/python@3.9/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/context.py", line 284, in _Popen
    return Popen(process_obj)
  File "/usr/local/Cellar/python@3.9/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/popen_spawn_posix.py", line 32, in __init__
    super().__init__(process_obj)
  File "/usr/local/Cellar/python@3.9/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/popen_fork.py", line 19, in __init__
    self._launch(process_obj)
  File "/usr/local/Cellar/python@3.9/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/popen_spawn_posix.py", line 42, in _launch
    prep_data = spawn.get_preparation_data(process_obj._name)
  File "/usr/local/Cellar/python@3.9/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/spawn.py", line 154, in get_preparation_data
    _check_not_importing_main()
  File "/usr/local/Cellar/python@3.9/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/spawn.py", line 134, in _check_not_importing_main
    raise RuntimeError('''
RuntimeError: 
        An attempt has been made to start a new process before the
        current process has finished its bootstrapping phase.

        This probably means that you are not using fork to start your
        child processes and you have forgotten to use the proper idiom
        in the main module:

            if __name__ == '__main__':
                freeze_support()
                ...

        The "freeze_support()" line can be omitted if the program
        is not going to be frozen to produce an executable.
Traceback (most recent call last):
  File "/path/to/venv/script.py", line 5, in <module>
    import yfinance_cache as yf
  File "/path/to/venv/lib/python3.9/site-packages/yfinance_cache/__init__.py", line 3, in <module>
    from .yfc_dat import Period, Interval, AmbiguousComparisonException
  File "/path/to/venv/lib/python3.9/site-packages/yfinance_cache/yfc_dat.py", line 266, in <module>
    manager = Manager()
  File "/usr/local/Cellar/python@3.9/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/context.py", line 57, in Manager
    m.start()
  File "/usr/local/Cellar/python@3.9/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/managers.py", line 558, in start
    self._address = reader.recv()
  File "/usr/local/Cellar/python@3.9/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/connection.py", line 250, in recv
    buf = self._recv_bytes()
  File "/usr/local/Cellar/python@3.9/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/connection.py", line 414, in _recv_bytes
    buf = self._recv(4)
  File "/usr/local/Cellar/python@3.9/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/connection.py", line 383, in _recv
    raise EOFError
EOFError
@ValueRaider
Copy link
Owner

ValueRaider commented Jul 6, 2024

What version YFC? Latest release has big changes to download, so I'm curious if upgrading to latest, or downgrading to previous, helps.

@sonique6784
Copy link
Contributor Author

sonique6784 commented Jul 7, 2024

@ValueRaider thanks I tried to downgrade as suggested, but I still get the same error.
I tried with yfinance 0.2.40 (latest), 0.2.38, 0.2.36 (found in yfinance-cache dependencies), but no luck.

Perhaps you could share your configuration and module versions with pip list?

$ python --version    
Python 3.9.19

$ pip list                    
Package               Version
--------------------- -----------
appdirs               1.4.4
beautifulsoup4        4.12.3
certifi               2024.7.4
charset-normalizer    3.3.2
click                 8.1.7
exchange_calendars    4.5.5
frozendict            2.4.4
html5lib              1.1
idna                  3.7
korean-lunar-calendar 0.3.1
lxml                  5.2.2
multitasking          0.0.11
numpy                 1.26.4
pandas                2.0.3
peewee                3.17.5
pip                   24.0
platformdirs          4.2.2
pyluach               2.2.0
python-dateutil       2.9.0.post0
pytz                  2024.1
requests              2.32.3
scipy                 1.13.1
setuptools            69.2.0
six                   1.16.0
soupsieve             2.5
toolz                 0.12.1
tzdata                2024.1
urllib3               2.2.2
webencodings          0.5.1
yfinance              0.2.36
yfinance-cache        0.6.4

@ValueRaider
Copy link
Owner

Python 3.12. It's more likely to be how multiprocessing works on Macs than package versions - I dev on Linux. You aren't first to encounter issues on Mac

@sonique6784
Copy link
Contributor Author

I tried with Python 3.12, but the issue is the same. Sounds like it is specific to Mac.
I will run my program in a Docker for now.

FYI

$ python --version
Python 3.12.3

$ pip list
Package               Version
--------------------- -----------
appdirs               1.4.4
beautifulsoup4        4.12.3
certifi               2024.7.4
charset-normalizer    3.3.2
click                 8.1.7
exchange_calendars    4.5.5
frozendict            2.4.4
html5lib              1.1
idna                  3.7
korean-lunar-calendar 0.3.1
lxml                  5.2.2
multitasking          0.0.11
numpy                 1.26.4
pandas                2.0.3
peewee                3.17.5
pip                   24.0
platformdirs          4.2.2
pyluach               2.2.0
python-dateutil       2.9.0.post0
pytz                  2024.1
requests              2.32.3
scipy                 1.14.0
six                   1.16.0
soupsieve             2.5
toolz                 0.12.1
tzdata                2024.1
urllib3               2.2.2
webencodings          0.5.1
yfinance              0.2.40
yfinance-cache        0.6.4

@sonique6784 sonique6784 changed the title Unable to run yfinance-cache: python multiprocessing errors macOS: Unable to run yfinance-cache: python multiprocessing errors Jul 7, 2024
@louiskenyon98
Copy link

I have the same issue on a windows machine.

@ValueRaider
Copy link
Owner

I don't have access to Windows nor Mac. Most changes in #46 were from ChatGPT/Claude, so try getting them to fix. There isn't much parallel code to feed the AI:

  • process launch
    if progress:
    queue = multiprocessing.Manager().Queue()
    partial_func = partial(download_one_parallel, queue=queue,
    period=period, interval=interval,
    max_age=max_age,
    start=start, end=end, prepost=prepost,
    actions=actions, adjust_divs=adjust_divs,
    adjust_splits=adjust_splits, keepna=keepna,
    proxy=proxy,
    rounding=rounding, session=session)
    with multiprocessing.Pool(processes=threads, initializer=reinitialize_locks, initargs=(yfcd.exchange_locks,)) as pool:
    result_async = pool.map_async(partial_func, tickers)
    if have_tqdm:
    r = tqdm.tqdm(tickers)
    else:
    r = range(len(tickers))
    for i in r:
    status, value = queue.get() # Blocks until a value is available
    if status == 'error':
    e, tb = value
    print(tb)
    sys.exit(1)
    if not have_tqdm:
    yfcu.display_progress_bar(i + 1, len(tickers))
    results = result_async.get()
    else:
    partial_func = partial(download_one,
    period=period, interval=interval,
    max_age=max_age,
    start=start, end=end, prepost=prepost,
    actions=actions, adjust_divs=adjust_divs,
    adjust_splits=adjust_splits, keepna=keepna,
    proxy=proxy,
    rounding=rounding, session=session)
    with multiprocessing.Pool(processes=threads) as pool:
    results = pool.map(partial_func, tickers)
  • safety locks
    from multiprocessing import Lock, Manager, current_process
    if current_process().name == 'MainProcess':
    # Ensure only main (parent) processes creates a manager
    manager = Manager()
    exchange_locks = {e:manager.Lock() for e in exchangeToXcalExchange.keys()}
    else:
    exchange_locks = {}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants