Python3多处理池和锁错误

2024-10-01 00:34:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我有如下所示的类似代码,所以请我想要正确的解决方案来运行这个没有错误,我得到了共享内存错误,并且多次打开gui

mainapp = ProcessFiles()
p = multiprocessing.Pool()
p.map(mainapp.getPdfInfo, Files_list)
p.close()

class ProcessFiles:

    def __init__():
        self.lock = multiprocessing.Lock()

    def getPdfInfo(file):
        #READ FILES DATA AND DO SOME STUFFS
        self.lock.aquere()
        #INSERT DATA TO DATABASE
        self.lock.release()

这就是错误消息

 TypeError: can't pickle sqlite3.Connection objects

Alos尝试了multiprocessing.Manager(),但也出现了错误,代码如下所示

mainapp = ProcessFiles()
p = multiprocessing.Pool()
p.map(mainapp.getPdfInfo, Files_list)
p.close()

class ProcessFiles:

    def __init__():
        m = multiprocessing.Manager()
        self.lock = m.Lock()

    def getPdfInfo(file):
        #READ FILES DATA AND DO SOME STUFFS
        self.lock.acquire()
        #INSERT DATA TO DATABASE
        self.lock.release()

这就是错误

 Traceback (most recent call last):
 File "<string>", line 1, in <module>
 File "C:\Python37\lib\multiprocessing\spawn.py", line 105, in spawn_main
 exitcode = _main(fd)
 ++++++++++++++++++++++++++++++
 _check_not_importing_main()
 File "C:\Python37\lib\multiprocessing\spawn.py", line 136, in 
 _check_not_importing_main
 is not going to be frozen to produce an executable.''')
 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.

Tags: thetoinselflockdatamaindef