Threading module

Hello everyone,

i’m using threading module to execute functions asynchronous.

all functions are wrapped in a while True loop for continous execution.
my issue is that functions are triggered from the while True loop, and executing again even if the previous execution is still in progress.
For example the while True is 0.5 sec and the function needs 4-5 seconds to be done.

How can i achieve something like this ? (Not re-execute the thread.start function if the previous .start() still running ?)

Thanks for any comments.

That does not make sense.

If the thread is doing work then it cannot start doing more work.

Maybe if you share code that shows what you are doing it will be easier to understand what you are doing.

Hello, yes of course.
Below are sample functions and modbus_pulse function taking 4 seconds.
All is executed with threading.start() and everything wrapped in infinite loop.

what i’m seeking is : not re-execute the modbus_pulse function if already there is an execution going on. (is actually executed every time from while True loop).

thanks for any suggestions.

import pymodbus
from pymodbus.client import ModbusTcpClient
from datetime import datetime
import random
import time
import threading


modbusdevice1=ModbusTcpClient('127.0.0.1', port=502)
modbusdevice2=ModbusTcpClient('127.0.0.1', port=502)
modbusdevice3=ModbusTcpClient('127.0.0.1', port=502)

counter=0



def modbus_pulse(ip='127.0.0.1' , port=502, address=10, slaveid=1 , length=3):

    modbusdevice=ModbusTcpClient(ip, port=port)
    state = True
    print(state)
    
    modbusdevice.write_registers(address,0,slaveid)
    time.sleep(0.2)
    modbusdevice.write_registers(address,1,slaveid)
    time.sleep(length)
    modbusdevice.write_registers(address,0,slaveid)
    time.sleep(2)
    state = False
    print(state)
    
    return None


def work1():
    
    modbusdevice1.write_registers(1,random.randint(0,32000),1)
    return None


def work2():
    modbusdevice1.write_registers(2,5,1)
    return None


def work3():
    modbusdevice2.write_registers(3,datetime.now().second,1)
    return None


def work4():
    global counter
    counter=counter+1
    modbusdevice2.write_registers(0,counter,1)
    return None



while True:
    

    wmp=threading.Thread(target=modbus_pulse)
    wmp.start()


    
    threading.Thread(target=work1).start()
    threading.Thread(target=work2).start()
    threading.Thread(target=work3).start()
    threading.Thread(target=work4).start()
    
    time.sleep(0.2)

Moved this to Python Help for more visibility and removed the free-threading tag because it’s not related to PEP 703.

i have tried to check is_alive before executing and now is not working in parallel.

"""
2024 09 25
"""
import time
import threading
from datetime import datetime




def f1():
    print('Hello ... 11');
    time.sleep(2);
    print('Hello ... 12'); 
    time.sleep(2);
    print('Hello .... 13'); 
    time.sleep(2);
    print('Hello .... end 14'); 
    time.sleep(2);
    print('f1 finish')
    

def f2():
    print('........World!..21'); 
    time.sleep(1);
    print('....... World!22'); 
    time.sleep(1);
    print('......World!..23'); 
    time.sleep(1);
    print('....... World!24'); 
    time.sleep(1);
    print('.......World!..25'); 
    time.sleep(1);
    print('........World! end 26'); 
    time.sleep(1);
    print('f2 finish')


while True:


    t1=threading.Thread(target=f1())
    if t1.is_alive()==False:
        t1.run()


    t2=threading.Thread(target=f2())
    if t2.is_alive()==False:
        t2.run()


    time.sleep(0.5)

Result:
Hello … 11
Hello … 12
Hello … 13
Hello … end 14
f1 finish
…World!..21
… World!22
…World!..23
… World!24
…World!..25
…World! end 26
f2 finish
Hello … 11
Hello … 12

try Lock Objects of threading :face_with_raised_eyebrow:

start() and run() are different.

Basically

start() calls run() in a new thread, whereas calling run directly does the thing in the same thread. Which is exactly what you’ve noticed happening.

Swap .run() for .start() in your second example.