1

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

1 Answer 1

2

You should know two things before you start:

  1. In Python there isn't multithreading actually because python execution architecture doesn't allow multithreading, i recommend you Linux C for that things.
  2. The execution order isn't guarateed, the scheduler executes in the order that it wants, so you use some blocking mechanism such semaphores to "actually" make it execute in the order you want, thats because you doesn't know when some line are going to be executed, and sometimes you need have control about system resources.

A key concept to have in mind is that threadsas asynchronus, maybe one can finish while another is running nevermind when it was created, so you have IPCs (Inter proces comunication) mechanism for actually do the proper with process and threads.

Imagine you make a request to a website and your PC Freezes until the request executes, that would be annoying, so threads arent for synchronous task.

So, that is the answer why you are not seeing the outputs in order.

Now let's tackle the duplicated error:

Because executions order isn't guaranteed and you are accesing a global variable without a protection, you are getting non-wanted errors, like duplicated numbers.

You need to use some blocking technique for read shared resourses.

Sign up to request clarification or add additional context in comments.

2 Comments

i'm kinda new to threading, i'm reusing 5 because i wants 5 threads and thread id=5 to handle multiple functions. Maybe that where i don't understand
The problem is you are accesing one memory address with no protection, image one or more threads are actually reading 10 numbres of the list and then printing, so when they fetch data, they get the same 10 numbers.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.