omnilib / aiomultiprocess

Take a modern Python codebase to the next level of performance.
https://aiomultiprocess.omnilib.dev
MIT License
1.77k stars 101 forks source link

RuntimeError: #165

Open 007-JB opened 2 years ago

007-JB commented 2 years ago
C:\Users\jbard\PycharmProjects\Parallel>C:\Users\jbard\PycharmProjects\Parallel\venv\Scripts\python.exe C:/Users/jbard/PycharmProjects/Parallel/aiomultiprocess_attempt.py
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Users\jbard\AppData\Local\Programs\Python\Python39\lib\multiprocessing\spawn.py", line 116, in spawn_main
    exitcode = _main(fd, parent_sentinel)
  File "C:\Users\jbard\AppData\Local\Programs\Python\Python39\lib\multiprocessing\spawn.py", line 125, in _main
    prepare(preparation_data)
  File "C:\Users\jbard\AppData\Local\Programs\Python\Python39\lib\multiprocessing\spawn.py", line 236, in prepare
    _fixup_main_from_path(data['init_main_from_path'])
  File "C:\Users\jbard\AppData\Local\Programs\Python\Python39\lib\multiprocessing\spawn.py", line 287, in _fixup_main_from_path
    main_content = runpy.run_path(main_path,
  File "C:\Users\jbard\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 268, in run_path
    return _run_module_code(code, init_globals, run_name,
  File "C:\Users\jbard\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 97, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "C:\Users\jbard\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\Users\jbard\PycharmProjects\Parallel\aiomultiprocess_attempt.py", line 23, in <module>
    asyncio.run(run_())
  File "C:\Users\jbard\AppData\Local\Programs\Python\Python39\lib\asyncio\runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "C:\Users\jbard\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 642, in run_until_complete
    return future.result()
  File "C:\Users\jbard\PycharmProjects\Parallel\aiomultiprocess_attempt.py", line 15, in run_
    async with Pool() as pool:
  File "C:\Users\jbard\PycharmProjects\Parallel\venv\lib\site-packages\aiomultiprocess\pool.py", line 186, in __init__
    self.init()
  File "C:\Users\jbard\PycharmProjects\Parallel\venv\lib\site-packages\aiomultiprocess\pool.py", line 214, in init
    self.processes[self.create_worker(qid)] = qid
  File "C:\Users\jbard\PycharmProjects\Parallel\venv\lib\site-packages\aiomultiprocess\pool.py", line 251, in create_worker
    process = PoolWorker(
  File "C:\Users\jbard\PycharmProjects\Parallel\venv\lib\site-packages\aiomultiprocess\pool.py", line 59, in __init__
    super().__init__(
  File "C:\Users\jbard\PycharmProjects\Parallel\venv\lib\site-packages\aiomultiprocess\core.py", line 109, in __init__
    namespace=get_manager().Namespace(),
  File "C:\Users\jbard\PycharmProjects\Parallel\venv\lib\site-packages\aiomultiprocess\core.py", line 29, in get_manager
    _manager = context.Manager()
  File "C:\Users\jbard\AppData\Local\Programs\Python\Python39\lib\multiprocessing\context.py", line 57, in Manager
    m.start()
  File "C:\Users\jbard\AppData\Local\Programs\Python\Python39\lib\multiprocessing\managers.py", line 554, in start
    self._process.start()
  File "C:\Users\jbard\AppData\Local\Programs\Python\Python39\lib\multiprocessing\process.py", line 121, in start
    self._popen = self._Popen(self)
  File "C:\Users\jbard\AppData\Local\Programs\Python\Python39\lib\multiprocessing\context.py", line 327, in _Popen
    return Popen(process_obj)
  File "C:\Users\jbard\AppData\Local\Programs\Python\Python39\lib\multiprocessing\popen_spawn_win32.py", line 45, in __init__
    prep_data = spawn.get_preparation_data(process_obj._name)
  File "C:\Users\jbard\AppData\Local\Programs\Python\Python39\lib\multiprocessing\spawn.py", line 154, in get_preparation_data
    _check_not_importing_main()
  File "C:\Users\jbard\AppData\Local\Programs\Python\Python39\lib\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.
**Code:**

import asyncio
from aiohttp import request
from aiomultiprocess import Pool
from time import time, perf_counter

async def get(url):
    async with request("GET", url) as response:
        return await response.text("utf-8")

async def run_():
    global Results
    Results = []
    start_t = perf_counter()
    async with Pool() as pool:
        async for result in pool.map(get, urls): Results.append(result)
    print(f'duration: {time.perf_counter()-start_t}')

urls = ['https://jsonplaceholder.typicode.com/todos/i' for i in range(4000)]
asyncio.run(run_())
print(Results)

OS: Windows Python: 3.9 aiomultiprocess version: 0.9.0

harrystuart commented 1 year ago

I have the same error - any fixes?