Python Forum
[SOLVED] Help with Threads - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: [SOLVED] Help with Threads (/thread-34350.html)



[SOLVED] Help with Threads - michaelserra - Jul-22-2021

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]



RE: Help with Threads - deanhystad - Jul-22-2021

Your limit thread will exit as soon as len(li) <= 5. Probably immediately.


RE: Help with Threads - snippsat - Jul-22-2021

You should not use global is terrible in almost all cases,and using threading dos not make it better.
Just to show some basic stuff with threading that may help.
from threading import Thread

def func1():
    print(f'Working')

def func2():
    print("Working")

if __name__ == '__main__':
    Thread(target=func1).start()
    Thread(target=func2).start()
If want argument to the function is done like(no value from global namespace) this with threading.
from threading import Thread
from time import sleep

def func1(name):
    sleep(5)
    print(f'Working {name}')

def func2():
    print("Working")

if __name__ == '__main__':
    name = 'Kent'
    Thread(target=func1, args=(name,)).start()
    Thread(target=func2).start()
See that working print immediately.
Without threading will now sleep block.
from threading import Thread
from time import sleep

def func1(name):
    sleep(5)
    print(f'Working {name}')

def func2():
    print("Working")

if __name__ == '__main__':
    name = 'Kent'
    func1(name)
    func2()



RE: Help with Threads - michaelserra - Aug-01-2021

(Jul-22-2021, 04:43 PM)deanhystad Wrote: Your limit thread will exit as soon as len(li) <= 5. Probably immediately.
Thank you! Thanks to this advice I found the error!