MultiProcessing Python无法在Windows中运行

zujrkrfu  于 2023-10-15  发布在  Python
关注(0)|答案(1)|浏览(175)
Traceback (most recent call last):   File
   "C:\Users\HP\Downloads\1\main.py", line 27, in <module>
       p.start()   File "C:\Users\HP\AppData\Local\Programs\Python\Python38-32\lib\multiprocessing\process.py",
   line 121, in start
       self._popen = self._Popen(self)   File "C:\Users\HP\AppData\Local\Programs\Python\Python38-32\lib\multiprocessing\context.py",
   line 224, in _Popen
       return _default_context.get_context().Process._Popen(process_obj)   File
   "C:\Users\HP\AppData\Local\Programs\Python\Python38-32\lib\multiprocessing\context.py",
   line 326, in _Popen
       return Popen(process_obj)   File "C:\Users\HP\AppData\Local\Programs\Python\Python38-32\lib\multiprocessing\popen_spawn_win32.py",
   line 93, in _init_
       reduction.dump(process_obj, to_child)   File "C:\Users\HP\AppData\Local\Programs\Python\Python38-32\lib\multiprocessing\reduction.py",
   line 60, in dump
       ForkingPickler(file, protocol).dump(obj) pickle.PicklingError: Can't pickle <function <lambda> at 0x039E9538>: attribute lookup
   <lambda> on __main_ failed

   C:\Users\HP\Downloads\1>Traceback (most recent call last):   File
   "<string>", line 1, in <module>   File
   "C:\Users\HP\AppData\Local\Programs\Python\Python38-32\lib\multiprocessing\spawn.py",
   line 102, in spawn_main
       source_process = _winapi.OpenProcess( OSError: [WinError 87] The parameter is incorrect

“”我试图同时打开多个chromedrivers/files.py。但它显示了错误。代码是从www.example.com获取示例file.py,并创建多个相同的文件,如file_1.py.然后运行所有已创建但不工作的文件。
代码是:

for proxy in file:
    shutil.copyfile('/home/hp/Documents/pr/proxy_project/template.py', '/home/hp/Documents/pr/proxy_project/file_{}.py'.format(i))
    file_name = "file_{}".format(i)
    file_list.append(file_name)
    with open('/home/hp/Documents/pr/proxy_project/file_{}.py'.format(i), 'r') as replicated_file:
        data = replicated_file.readlines()
    data_to_be_added = "proxy = '{}'\n".format(proxy.strip("\n"))
    data[16] = data_to_be_added
    with open('/home/hp/Documents/pr/proxy_project/file_{}.py'.format(i), 'w') as new_replicated_file:
        new_replicated_file.writelines(data)
    i += 1

for file_py in tuple(file_list):
    print("File name = {}".format(file_py))
    p = multiprocessing.Process(target=lambda: __import__(file_py))
    p.start()

当这段代码运行时,还有另一个文件将被复制

ldioqlga

ldioqlga1#

当使用多处理时,你必须保护你的主代码,

if __name__== '__main__':

这在多处理编程指南中有详细说明,特别是在“安全导入主模块”下。

相关问题