嗨,我现在刚接触多线程并试图解决这个问题:
在开始时,我只是尝试使用线程打印:
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似乎线程不是按照打印的顺序运行的。
希望有人能帮我解决这个问题
发布于 2020-08-12 05:37:47
在你开始之前,你应该知道两件事:
Python中的
需要记住的一个关键概念是线程异步,也许在创建时,一个线程可以完成,而另一个线程在运行时不要紧,所以您有IPC()机制,用于对进程和线程进行适当的操作。
假设您向某个网站发出请求,而您的PC一直冻结到请求执行为止,这将是很烦人的,因此线程不能执行同步任务。
所以,这就是为什么你没有按顺序看到输出的原因。
现在让我们来处理重复错误:
因为执行命令不能得到保证,而且没有保护就可以访问全局变量,所以您将得到一些不需要的错误,比如重复的数字。
您需要使用一些阻塞技术来读取共享资源。
https://stackoverflow.com/questions/63370139
复制相似问题