首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >python3多线程写入文件

python3多线程写入文件
EN

Stack Overflow用户
提问于 2020-08-12 05:08:58
回答 1查看 52关注 0票数 1

嗨,我现在刚接触多线程并试图解决这个问题:

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

在开始时,我只是尝试使用线程打印:

代码语言:javascript
复制
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")

印得很完美:

代码语言:javascript
复制
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

但当我试图写文件时:

代码语言:javascript
复制
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")

我的文件中的内容是错误的,并且有复制,如下所示:

代码语言:javascript
复制
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

似乎线程不是按照打印的顺序运行的。

希望有人能帮我解决这个问题

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-12 05:37:47

在你开始之前,你应该知道两件事:

Python中的

  1. 实际上并不存在多线程,因为python执行架构不允许多线程,为此我建议您使用Linux C。
  2. --执行顺序没有受到保护,调度程序按照它想要的顺序执行,所以您可以使用一些阻塞机制,这样的信号量“实际上”按您想要的顺序执行,这是因为您不知道什么时候要执行某些行,有时您需要控制系统资源。

需要记住的一个关键概念是线程异步,也许在创建时,一个线程可以完成,而另一个线程在运行时不要紧,所以您有IPC()机制,用于对进程和线程进行适当的操作。

假设您向某个网站发出请求,而您的PC一直冻结到请求执行为止,这将是很烦人的,因此线程不能执行同步任务。

所以,这就是为什么你没有按顺序看到输出的原因。

现在让我们来处理重复错误:

因为执行命令不能得到保证,而且没有保护就可以访问全局变量,所以您将得到一些不需要的错误,比如重复的数字。

您需要使用一些阻塞技术来读取共享资源。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63370139

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档