Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中的多处理导致数据中断问题_Python_Python 3.x_Python 2.7 - Fatal编程技术网

python中的多处理导致数据中断问题

python中的多处理导致数据中断问题,python,python-3.x,python-2.7,Python,Python 3.x,Python 2.7,我正在线程中运行python代码,使用多处理模块将输入数据(~2个源文件)写入单个目标文件 然而,在我编写时,目标文件中的一些记录被破坏(100k记录中有400条记录被破坏) 所谓破记录,我的意思是,输出记录预计为: “第1栏”、“第2栏”、“第3栏”、“第4栏”、“第4栏” 然而,它被破坏为: mn3、第4栏、第4栏 请提供一些帮助,我的目标文件上是否存在多个进程正在创建的锁定?如何避免这种情况?或者是否有其他解决方案 def process_source_file_data(in_fi

我正在线程中运行python代码,使用多处理模块将输入数据(~2个源文件)写入单个目标文件

然而,在我编写时,目标文件中的一些记录被破坏(100k记录中有400条记录被破坏)

所谓破记录,我的意思是,输出记录预计为:

“第1栏”、“第2栏”、“第3栏”、“第4栏”、“第4栏”

然而,它被破坏为:

mn3、第4栏、第4栏

请提供一些帮助,我的目标文件上是否存在多个进程正在创建的锁定?如何避免这种情况?或者是否有其他解决方案




def process_source_file_data(in_file_name,lock):
     lock.acquire()     
     "written some code to write data from 'in_file_name' to a single target csv"
     lock.release()


if __name__ == "__main__":

    file_name_1 = sys.argv[1]
    file_name_2 = sys.argv[2]
    lock1 =multiprocessing.Lock()
    lock2 =multiprocessing.Lock()


    p1 = multiprocessing.Process(target=process_source_file_data, args=(file_name_1,lock1))
    p2 = multiprocessing.Process(target=process_source_file_data, args=(file_name_2,lock2))


    p1.start()
    p2.start()



    p1.join()
    p2.join()


您需要为在进程之间共享的每个资源使用锁,这与示例中每个进程的锁相反

from multiprocessing import Process, Lock

def f(i, l):
    l.acquire()
    try:
        print('hello world', i)
        print('hello world', i)
    finally:
        l.release()

if __name__ == '__main__':
    lock = Lock()

    for num in range(10):
        Process(target=f, args=(num, lock)).start()

RESULT:
hello world 1
hello world 1
hello world 4
hello world 0
hello world 4
hello world 2
hello world 0
hello world 2
hello world 3
hello world 3
hello world 5
hello world 5
hello world 7
hello world 7
hello world 6
hello world 6
hello world 9
hello world 9
hello world 8
hello world 8
为每个进程使用锁将不允许在进程之间传输锁获取信息:

from multiprocessing import Process, Lock

def f(i, l):
    l.acquire()
    try:
        print('hello world', i)
        print('hello world', i)
    finally:
        l.release()

if __name__ == '__main__':

    for num in range(10):
        lock = Lock()
        Process(target=f, args=(num, lock)).start()
这将导致:

hello world 5
hello world 2
hello world 2
hello world 5
hello world 1
hello world 1
hello world 0
hello world 0
hello world 4
hello world 4
hello world 3
hello world 3
hello world 6
hello world 6
hello world 7
hello world 7
hello world 8
hello world 8
hello world 9
hello world 9

当您在没有多线程的情况下运行程序时,是否会获得相同的中断?我不会,但性能非常差,如果您没有在单个线程中获得相同的中断,那么它可能与最终结果的连接方式有关。如果我在每个进程中使用相同的锁,则与运行python脚本w相同不使用多进程。每个进程都必须等待直到释放前一个锁,但之后您可以并行进行计算。当您对整个进程使用锁时,您不会真正“锁定”“由于另一个进程仍可以访问该文件并写入,因此无法删除该文件。为什么不将输入文件中的数据保存到变量中,并每隔一段时间只写入该文件一次呢?