Search code examples
pythonmultithreadingglobal-variables

Passing thread names as gloabl variables to a function


I'm a bit stuck, i'm trying to pass thread names given by the system to my function so that i can print the start and end time of the current thread working in the function, i'm using global variables name for that. The user has to input a number in the given interval. The thread names work fine when i inputed 1001 but if i input numbers like 1200 or 10001 the names do not fit anymore. I put examples of the output, output 1 is not what i'm looking, output 2 is what i need. I'm not sure what is causing the name change. If any additional information is needed i'm happy to provide it

import os
from posixpath import abspath
import time
import sys
import signal
import threading
import platform
import subprocess
from pathlib import Path
import math
lokot = threading.Lock()
lista = []
name = 0
name3 = 0
def divisor(start,end):
    lokot.acquire()
    start = time.time()
    print('{} started working at {}'.format(name, start))
    for i in range(int(start),int(end)+1):
        if int(end) % i == 0:
            lista.append(i)
    end = time.time()
    print('{} ended working at {}'.format(name, end))
    lokot.release()
def new_lista():
    lokot.acquire()
    start = time.time()
    nlista = []
    for i in lista:
        if i % 2 == 0:
            nlista.append(i)
    print(nlista)
    print('{} was executed in time frame {}'.format(name3,time.time()-start))
    lokot.release()

def f4():
    while (True):
        print ('Input a non negative number in given range <1000,200000>')
        number = input()
        if number.isalpha() or not number or int(number) not in range(1000,200000) :
            print('Number entered is not in the interval <1000,200000>')
            continue
        else:
            global name
            global name3
            x = int(number) / 2
            t1 = threading.Thread(target=divisor, args=(1, x))
            t2 = threading.Thread(target=divisor, args=(1, number))
            t3 = threading.Thread(target=nova_lista)
            name = t1.name
            t1.start()
            name = t2.name
            t2.start()
            name3 = t3.name
            t3.start()
            t1.join()
            t2.join()
            t3.join()
            break

Input 1:
100001
Output 1:
Thread-1 started working at 1624538800.4813018
Thread-2 ended working at 1624538800.4887686
Thread-2 started working at 1624538800.4892647
Thread-2 ended working at 1624538800.5076165
[2, 4, 8, 10, 16, 20, 40, 50, 80, 100, 200, 250, 400, 500, 1000, 1250, 2000, 2500, 5000, 6250, 10000, 12500, 25000, 50000]
Thread-3 dwas executed in time frame 0.0
Input 2:
1001
Output 2:
Thread-1 started working at 1624538882.90607
Thread-1 ended working at 1624538882.9070616
Thread-2 started working at 1624538882.9074266
Thread-2 ended working at 1624538882.9089162
[2, 4, 8, 10, 20, 40, 50, 100, 200, 250, 500, 1000, 1250, 2500, 5000]
Thread-3 dretva se izvodila 0.0

Solution

  • This won't necessarily work:

    name = t1.name
    t1.start()
    name = t2.name
    

    Nothing prevents the second assignment from happening before the t1 thread accesses the name variable.

    Q: Why don't you just assign names when you create the threads instead of letting the Threading library assign them? E.g.;

    t1 = threading.Thread(target=divisor, args=(1, x), name="t1")