I hope that the first three threads, after complete task1, timing + 1, and then, after complete wait_task timing plus 1, then all finished task3 in turn, timing plus 1, then complete task4, timing plus 1.
Code below, however the results is not I think so, which where bosses can point out problems, and help to correct?
The from threading import Thread
The import threading
Cond=threading. Condition ()
Def the main () :
Global total_current_time, total_last_time
Total_current_time=0
Total_last_time=0
Global robot_tasks_dict
Global flag
Robot_tasks_dict=dict ()
# robot_steps_finished=False
Robot_task_list=[]
Count_time_thread=Thread (target=count_time, args=())
For I in range (3) :
Robot_task_thread=Thread (target=robot_task, args=(I,))
Robot_task_list. Append (robot_task_thread)
Count_time_thread. Start ()
For the thread in robot_task_list:
Thread. The start ()
Def count_time () :
Print (type (cond))
Cond. Acquire ()
Global flag
Flag=False
Values=robot_tasks_dict. Values ()
Global total_last_time
Global total_current_time
Result=1
For I in values:
Result=result * I
Print (' result=% d % result)
If the result==1:
Total_last_time=total_current_time
Total_current_time +=1
Print (" -- -- -- -- -- total_current_time=% d '% total_current_time)
Flag=True
Cond. Notify_all ()
Cond. Wait ()
Cond. Release ()
Def robot_task (robotid) :
Task1 (robotid)
Wait_task (robotid)
Task2 (robotid)
Task3 (robotid)
Def task1 (robotid) :
Cond. Acquire ()
Global flag
The if flag==False:
Cond. Wait ()
Global robot_tasks_dict
Robot_tasks_dict [threading current_thread (). The name]=0
Print (" -- -- -- -- -- -- -- -- -- -- robot [% s] task1 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- '% robotid)
Robot_tasks_dict [threading current_thread (). The name]=1
Cond. Notify_all ()
Cond. Wait ()
Cond. Release ()
Def task2 (robotid) :
Cond. Acquire ()
Global flag
The if flag==False:
Cond. Wait ()
Global robot_tasks_dict
Robot_tasks_dict [threading current_thread (). The name]=0
Print (" -- -- -- -- -- -- -- -- -- -- robot [% s] task2 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- '% robotid)
Robot_tasks_dict [threading current_thread (). The name]=1
Cond. Notify_all ()
Cond. Wait ()
Cond. Release ()
Def task3 (robotid) :
Cond. Acquire ()
Global flag
The if flag==False:
Cond. Wait ()
Global robot_tasks_dict
Robot_tasks_dict [threading current_thread (). The name]=0
Print (" -- -- -- -- -- -- -- -- -- -- robot [% s] task3 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- '% robotid)
Robot_tasks_dict [threading current_thread (). The name]=1
Cond. Notify_all ()
Cond. Wait ()
Cond. Release ()
Def wait_task (robotid) :
Cond. Acquire ()
Global flag
The if flag==False:
Cond. Wait ()
Global robot_tasks_dict
Robot_tasks_dict [threading current_thread (). The name]=0
Print (" -- -- -- -- -- -- -- -- -- -- robot [% s] wait task -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- '% robotid)
Robot_tasks_dict [threading current_thread (). The name]=1
Cond. Notify_all ()
Cond. Wait ()
Cond. Release ()
If __name__=="__main__ ':
The main ()
CodePudding user response:
Use state control
The from threading import Thread
The import threading
The import time
Def the main () :
Global task1_pass, wait_pass task2_pass, task3_pass
Task1_pass=False
Wait_pass=False
Task2_pass=False
Task3_pass=False
Global total_current_time, total_last_time
Total_current_time=0
Total_last_time=0
Global robot_tasks_dict
Global flag
Robot_tasks_dict={0-0, 1-0, 2-0}
Robot_task_list=[]
Count_time_thread=Thread (target=count_time, args=())
For I in range (3) :
Robot_task_thread=Thread (target=robot_task, args=(I,))
Robot_task_list. Append (robot_task_thread)
Count_time_thread. Start ()
For the thread in robot_task_list:
Thread. The start ()
Def count_time () :
Global total_last_time
Global total_current_time
Global task1_pass, wait_pass task2_pass, task3_pass
While not task3_pass:
Values=robot_tasks_dict. Values ()
Result=1
For I in values:
Result=result * I
# print (' result=% d % result)
If the result==1:
Total_last_time=total_current_time
Total_current_time +=1
Print (" -- -- -- -- -- total_current_time=% d '% total_current_time)
For k in robot_tasks_dict. Keys () :
Robot_tasks_dict [k]=0
If task2_pass: task3_pass=True
Elif wait_pass: task2_pass=True
Elif task1_pass: wait_pass=True
The else: task1_pass=True
Def robot_task (robotid) :
Task1 (robotid)
Wait_task (robotid)
Task2 (robotid)
Task3 (robotid)
Def task1 (robotid) :
Global task1_pass
Global robot_tasks_dict
Robot_tasks_dict [robotid]=0
Print (" -- -- -- -- -- -- -- -- -- -- robot [% s] task1 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- '% robotid)
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull