'Multiprocessing code executes properly under PyCharm with Debug button but gets error "Can't pickle <function add ..." with Run button
Environment:
- macOS Big Sur 11.2.3
- Python 3.8.10
- PyCharm 2021.2.1 Professional Edition
When I using multiprocessing module with python3.8.10 and click Debug button run:
import multiprocessing
def add(a: int, b: int):
return a + b
if __name__ == '__main__':
pool = multiprocessing.Pool(2)
multi_result = []
# begin
for i in range(20):
multi_result.append(pool.apply_async(func=add, args=(i, i + 1)))
pool.close()
pool.join()
# print result
for _r in multi_result:
print(str(_r.get()))
I got correct result:
1
3
5
7
...
While I use Run button, something wrong and I cant get results:
runfile('/Users/xq/Desktop/data/pycharm_pro/Test_alg/other_test/multi_get_result.py', wdir='/Users/xq/Desktop/data/pycharm_pro/Test_alg/other_test')
Traceback (most recent call last):
File "/Users/xq/.conda/envs/pycharm_env/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3441, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-2-b38026d4d0fe>", line 1, in <module>
runfile('/Users/xq/Desktop/data/pycharm_pro/Test_alg/other_test/multi_get_result.py', wdir='/Users/xq/Desktop/data/pycharm_pro/Test_alg/other_test')
File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydev_bundle/pydev_umd.py", line 198, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "/Users/xq/Desktop/data/pycharm_pro/Test_alg/other_test/multi_get_result.py", line 18, in <module>
print(str(_r.get()))
File "/Users/xq/.conda/envs/pycharm_env/lib/python3.8/multiprocessing/pool.py", line 771, in get
raise self._value
File "/Users/xq/.conda/envs/pycharm_env/lib/python3.8/multiprocessing/pool.py", line 537, in _handle_tasks
put(task)
File "/Users/xq/.conda/envs/pycharm_env/lib/python3.8/multiprocessing/connection.py", line 206, in send
self._send_bytes(_ForkingPickler.dumps(obj))
File "/Users/xq/.conda/envs/pycharm_env/lib/python3.8/multiprocessing/reduction.py", line 51, in dumps
cls(buf, protocol).dump(obj)
_pickle.PicklingError: Can't pickle <function add at 0x7fb2204665e0>: attribute lookup add on __main__ failed
Why? Any ideas?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
