T
T
Tahircik2018-09-24 05:05:59
Python
Tahircik, 2018-09-24 05:05:59

Threads in python?

Two threads are accessing the same list. One sees it empty, and the other shows that there is data in it. Why is this happening and how to deal with it.
this is not the whole code, but I’ll try to explain, execute adds a function to the list, and run (runs in a separate thread) executes and writes the answer to the dictionary, the problem is that the execute function adds to the list, but from the thread where run the list remains empty. I will say in advance with Queue a similar situation

tail = []
response = {}



def run():
    print(123)
    while True:
        try:
            if len(tail) > 0:
                for i in range(95):
                    func = tail.pop()
                    response.update({id(func) : func()})
                    time.sleep(1)
        except IndexError:
            pass
        except Exception as e:
            print(e)

def execute(self , func):
    global tail #что с global что без , тоже самое
    tail.append(func)
    while True:
        if response.get(id(func)) != None:
            return response.get(id(func))

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question