Hi i'm currently new to multithreading and trying to solve this problem:
- have a list of 75 element
- write to 8 files with no more than 10 elements of the list(ex: file1 - from index 0 to 9, file 2 - from index 10 to 19, ... , file 8- from index 70 to 74)
- use 5 threads to handle, each thread handle a separate file writing, 1 thread will handle more than 1 file
at the start i just try to print using 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] 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") and it printed out perfect:
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 but when i try to write to files:
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") the content in my files are wrong and have duplicate like the print following:
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 It seems like the thread are not running by order to print
Hope someone can help me figure out the problem