Multiple timer running at the same time from the same function in different threads

2

I want to use a function to launch a timer with different frequencies for each of the threads that I open. I do not know if this is possible. Could you help me out?

import threading
import logging
logger = logging.getLogger(__name__) 
lock = threading.Lock()

def f(freq):
    with lock:
        logger.info('f')
        threading.Timer(freq, f(freq)).start()
        print(freq)

if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG,
                    format='[%(asctime)s %(threadName)s] %(message)s',
                    datefmt='%H:%M:%S')
    freq = [5,10,15]
    for i in range(0, len(freq)):
    threading.Thread(target=f(freq[i])).start()

This is an example I'm trying. I execute a thread for each frequency, and I call the function to which the frequency happened. When I execute it, execution is blocked in the first timer. I think it's because the thread has the same target always, but how could I do it just to use a function that calls me caad timer? There are going to be a few executions and creating a function for each thread is unfeasible.

Thank you very much.

    
asked by Alejo 15.06.2018 в 18:56
source

1 answer

0

No. Your problem is that target has to be a function, and not a function call .

freq = [5,10,15]
for i in range(0, len(freq)):
    t = threading.Thread(target=f, args=(freq[i],,)).start()

You will receive the parameters, in this case, as a tuple that contains the values you need to pass to the function that will be executed in the thread corresponding.

    
answered by 15.06.2018 в 19:13