Jul-22-2021, 04:34 PM
(This post was last modified: Aug-01-2021, 02:23 PM by michaelserra.)
I'm hanging out with i/o bound stuff in Python and I created a little .py script wich contains three simply functions.
import threading import time li = [] item = 0 def add_item(): global item for i in range(7): item += 1 li.append(item) print("Added item!") time.sleep(1) print("Elements added succesfully!") def add_item_2(): global item for i in range(10): item += 1 li.append(item) print("Added item!") time.sleep(0.5) print("Elements added succesfully!") def limit_item(): while len(li) > 5: print("List overload: subtracking elements to reach 5 total elements") del li[-1] print(f"Actual elements: {len(li)}") add_item(); add_item_2(); limit_item() print(li)And it works as expected:
Output:Added item!
Added item!
Added item!
Added item!
Added item!
Added item!
Added item!
Elements added succesfully!
Added item!
Added item!
Added item!
Added item!
Added item!
Added item!
Added item!
Added item!
Added item!
Added item!
Elements added succesfully!
List overload: subtracking elements to reach 5 total elements
Actual elements: 16
List overload: subtracking elements to reach 5 total elements
Actual elements: 15
List overload: subtracking elements to reach 5 total elements
Actual elements: 14
List overload: subtracking elements to reach 5 total elements
Actual elements: 13
List overload: subtracking elements to reach 5 total elements
Actual elements: 12
List overload: subtracking elements to reach 5 total elements
Actual elements: 11
List overload: subtracking elements to reach 5 total elements
Actual elements: 10
List overload: subtracking elements to reach 5 total elements
Actual elements: 9
List overload: subtracking elements to reach 5 total elements
Actual elements: 8
List overload: subtracking elements to reach 5 total elements
Actual elements: 7
List overload: subtracking elements to reach 5 total elements
Actual elements: 6
List overload: subtracking elements to reach 5 total elements
Actual elements: 5
[1, 2, 3, 4, 5]
The problem is that when I convert the three functions to threads with the 'threading' module, basically the limiter()
function (so the t_limiter
thread) seems not to work. Here's the complete code:import threading import time li = [] item = 0 def add_item(): global item for i in range(7): item += 1 li.append(item) print("Added item!") time.sleep(1) print("Elements added succesfully!") def add_item_2(): global item for i in range(10): item += 1 li.append(item) print("Added item!") time.sleep(0.5) print("Elements added succesfully!") def limit_item(): while len(li) > 5: print("List overload: subtracking elements to reach 5 total elements") del li[-1] print(f"Actual elements: {len(li)}") t_adder1 = threading.Thread(target=add_item) t_adder2 = threading.Thread(target=add_item_2) t_limiter = threading.Thread(target=limit_item) t_limiter.start(); t_adder1.start(); t_adder2.start() t_adder1.join(); t_adder2.join(); t_limiter.join() print(li)I'm surely wrong but I thought that theoritically it should act everytime the length of the list exceeds 5 elements, but it seems not to be executed. Here's the output:
Output:Added item!
Added item!
Added item!
Added item!
Added item!
Added item!
Added item!
Added item!
Added item!
Added item!
Added item!
Added item!
Added item!
Added item!
Added item!
Added item!
Elements added succesfully!
Added item!
Elements added succesfully!
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]