gpt4 book ai didi

python - python3多线程写入文件

转载 作者:行者123 更新时间:2023-12-03 13:23:23 27 4
gpt4 key购买 nike

嗨,我目前是多线程新手,正在尝试解决此问题:

  • 包含75个元素的列表
  • 写入8个文件,且列表中的元素不超过10个(例如:file1-从索引0到9,文件2-从索引10到19,...,文件8-从索引70到74)
  • 使用5个线程来处理,每个线程处理一个单独的文件写入,一个线程将处理1个以上的文件

  • 一开始我只是尝试使用线程进行打印:
    import threading
    a = list(range(75))

    class myThread(threading.Thread):
    def __init__(self, threadID, name, file_number):
    threading.Thread.__init__(self)
    self.threadID = threadID
    self.name = name
    self.file_number = file_number

    def run(self):
    print("Starting " + self.name)
    print_time(self.name, self.file_number)
    print("Exiting " + self.name)

    def print_time(threadName, file_number):
    render_list = a[:10]
    print("%s: %s" % (threadName, render_list))
    del a[:10]


    # Create new threads
    thread1 = myThread(1, "Thread-1", 1)
    thread2 = myThread(2, "Thread-2", 2)
    thread3 = myThread(3, "Thread-3", 3)
    thread4 = myThread(4, "Thread-4", 4)
    thread5 = myThread(5, "Thread-5", 5)
    thread6 = myThread(5, "Thread-5", 6)
    thread7 = myThread(5, "Thread-5", 7)
    thread8 = myThread(5, "Thread-5", 8)

    # Start new Threads
    thread1.start()
    thread2.start()
    thread3.start()
    thread4.start()
    thread5.start()
    thread6.start()
    thread7.start()
    thread8.start()
    thread1.join()
    thread2.join()
    thread3.join()
    thread4.join()
    thread5.join()
    thread6.join()
    thread7.join()
    thread8.join()
    print("Exiting Main Thread")
    它打印出完美的:
    Starting Thread-1
    Thread-1: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    Exiting Thread-1
    Starting Thread-2
    Thread-2: [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
    Exiting Thread-2
    Starting Thread-3
    Thread-3: [20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
    Exiting Thread-3
    Starting Thread-4
    Thread-4: [30, 31, 32, 33, 34, 35, 36, 37, 38, 39]
    Exiting Thread-4
    Starting Thread-5
    Thread-5: [40, 41, 42, 43, 44, 45, 46, 47, 48, 49]
    Exiting Thread-5
    Starting Thread-5
    Thread-5: [50, 51, 52, 53, 54, 55, 56, 57, 58, 59]
    Exiting Thread-5
    Starting Thread-5
    Thread-5: [60, 61, 62, 63, 64, 65, 66, 67, 68, 69]
    Exiting Thread-5
    Starting Thread-5
    Thread-5: [70, 71, 72, 73, 74]
    Exiting Thread-5
    Exiting Main Thread
    但是当我尝试写入文件时:
    import threading
    a = list(range(75))

    class myThread(threading.Thread):
    def __init__(self, threadID, name, file_number):
    threading.Thread.__init__(self)
    self.threadID = threadID
    self.name = name
    self.file_number = file_number

    def run(self):
    print("Starting " + self.name)
    print_time(self.name, self.file_number)
    print("Exiting " + self.name)

    def print_time(threadName, file_number):
    render_list = a[:10]
    f = open("demofile_%s.txt" % file_number, "a")
    for i in render_list:
    f.write("%s\n" % i)
    f.close()
    print("%s: %s" % (threadName, render_list))
    del a[:10]


    # Create new threads
    thread1 = myThread(1, "Thread-1", 1)
    thread2 = myThread(2, "Thread-2", 2)
    thread3 = myThread(3, "Thread-3", 3)
    thread4 = myThread(4, "Thread-4", 4)
    thread5 = myThread(5, "Thread-5", 5)
    thread6 = myThread(5, "Thread-5", 6)
    thread7 = myThread(5, "Thread-5", 7)
    thread8 = myThread(5, "Thread-5", 8)

    # Start new Threads
    thread1.start()
    thread2.start()
    thread3.start()
    thread4.start()
    thread5.start()
    thread6.start()
    thread7.start()
    thread8.start()
    thread1.join()
    thread2.join()
    thread3.join()
    thread4.join()
    thread5.join()
    thread6.join()
    thread7.join()
    thread8.join()
    print("Exiting Main Thread")
    我文件中的内容有误,并且重复了以下打印内容:
    Starting Thread-1
    Starting Thread-2
    Starting Thread-3
    Starting Thread-4
    Thread-1: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    Exiting Thread-1
    Thread-3: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    Exiting Thread-3
    Thread-4: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    Exiting Thread-4
    Starting Thread-5
    Thread-2: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    Starting Thread-5
    Thread-5: [30, 31, 32, 33, 34, 35, 36, 37, 38, 39]
    Exiting Thread-2
    Exiting Thread-5
    Thread-5: [30, 31, 32, 33, 34, 35, 36, 37, 38, 39]
    Starting Thread-5
    Exiting Thread-5
    Starting Thread-5
    Thread-5: [60, 61, 62, 63, 64, 65, 66, 67, 68, 69]
    Thread-5: [60, 61, 62, 63, 64, 65, 66, 67, 68, 69]
    Exiting Thread-5
    Exiting Thread-5
    Exiting Main Thread
    似乎该线程未按打印顺序运行
    希望有人可以帮助我找出问题所在

    最佳答案

    在开始之前,您应该了解两件事:

  • 在Python中实际上没有多线程,因为python执行体系结构不允许多线程,因此我建议您使用LinuxC。
  • 不保证执行顺序,调度程序按它想要的顺序执行,因此您使用了某种阻塞机制,例如信号量,以“实际”使其按您想要的顺序执行,那是因为您不知道何时有任何行将要执行,有时您需要控制系统资源。

  • 要牢记的一个关键概念是线程是异步的,也许一个线程在创建时就可以完成,而另一个线程却在运行时没关系,因此您具有IPC(进程间通信)机制,可以对进程和线程进行正确的处理。
    想象一下,您向一个网站发出请求,并且您的PC冻结直到该请求执行为止,这很烦人,因此线程不同步任务。
    因此,这就是为什么您没有按顺序看到输出的答案。
    现在让我们解决重复的错误:
    因为不能保证执行顺序,并且您要访问没有保护的全局变量,所以您会收到不需要的错误,例如重复的数字。
    您需要使用某些阻止技术来读取共享资源。

    关于python - python3多线程写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63370139/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com