Python Forum
Multithreading dynamically syncronism - 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: Multithreading dynamically syncronism (/thread-22317.html)



Multithreading dynamically syncronism - Rodrigo - Nov-08-2019

Hi there, I have n threads named as: E1, E2, E3, ... And additionally another m number os threads named as: C1, C2, C3, ... The (C ) type threads are dependent from 2*(E) type threads. I want to dynamically syncronize (C ) threads to start just when the dependents (E) are finished. All threads are started with know name and I have a dictionary with respective dependencies. I know I doing it wrong at (Condicoes[i][j]).join() on attached code. I tried to iterate with threadsList but I dont know how to recover the (E) threads names to compare with my dict conditons. My ListaThreadEntradas has this: [<Thread(E1, stopped daemon 18288)>, <Thread(E2, stopped daemon 11428)>, <Thread(E3, stopped daemon 1908)>, <Thread(E4, stopped daemon 3436)>] If I can get those En values I could compare with my dependency dictionary. Can anyone help me out, please?
 # -*- coding: utf-8 -*- #!/usr/bin/env python import time import json import threading #time delay funcion def demora(Tempo = 0): time.sleep(Tempo) print ("wait "+str(Tempo)+"s") #Json with all possible En. It will be dynamically assign. EntradasString = '{ "E1" : "1", "E2" : "2", "E3" : "3", "E4" : "4" }' Entradas = json.loads(EntradasString) #Dynamic allocation of threads. ListaThreadEntradas = [] for i in (Entradas): x = threading.Thread(target=demora, name=i, args=(int(Entradas[i]),), daemon=True) ListaThreadEntradas.append(x) x.start() #Condition depend on two E type theads.It will be dynamically assign ListaCondicoes = ' {"C1" : {"0": "E1", "1": "E2"} } ' Condicoes = json.loads(ListaCondicoes) def dependente(): inicio = time.time() for i in (Condicoes): for j in ( Condicoes[i] ): (Condicoes[i][j]).join() fim = time.time() - inicio print ("Took: " + str(fim) + "s") C1 = threading.Thread(target=dependente, name="C1", args=(), deamon=True) C1.start()