Commit ba07274d763d7ad9a4445b2c93f5d270cf56c600
1 parent
b9811afe
Exists in
dev
Simplification du mode processus => presque aussi simple que thread !!!
Showing
3 changed files
with
39 additions
and
10 deletions
Show diff stats
README.md
... | ... | @@ -74,10 +74,13 @@ Author: E. Pallier |
74 | 74 | VERSION: 0.20.16 |
75 | 75 | |
76 | 76 | Comment: |
77 | + Simplification du mode processus (presque aussi simple que thread) | |
78 | + | |
77 | 79 | Bugfixes & New test |
78 | 80 | - Bugfix specific_process() en mode "processus" => meilleure gestion accès concurrent à la BD |
79 | 81 | - Bugfix Agent => fonctionne aussi en mode SIMU OFF |
80 | 82 | - Ajout d'un gros test à la fin pour s'assurer que les résultats sont conformes à l'attendu (only en mode simu) |
83 | + | |
81 | 84 | - Rappel : il suffit de mettre la constante RUN_IN_THREAD de AgentX à False ou True |
82 | 85 | |
83 | 86 | -------------------------------------------------------------------------------------------- | ... | ... |
src/agent/Agent.py
... | ... | @@ -24,9 +24,8 @@ from django import db |
24 | 24 | """TODO: |
25 | 25 | |
26 | 26 | - 1 log par agent + lastlog (30 dernières lignes) |
27 | -- 1 table par agent | |
28 | 27 | - table agents_log avec log minimum pour affichage dans dashboard, et ordre chrono intéressant pour suivi activité : nom agent, timestamp, message |
29 | -- table agent_<agent-name>_vars : nom variable, value, desc | |
28 | +- 1 table par agent: agent_<agent-name>_vars : nom variable, value, desc | |
30 | 29 | |
31 | 30 | """ |
32 | 31 | |
... | ... | @@ -537,11 +536,11 @@ class Agent: |
537 | 536 | print("Updating the survey database table...") |
538 | 537 | print("- fetching table line for agent", self.name) |
539 | 538 | # only necessary when using process (not necessary with threads) |
540 | - with transaction.atomic(): | |
541 | - self._agent_survey = AgentSurvey.objects.get(name=self.name) | |
542 | - self._agent_survey.mode = self.mode | |
543 | - self._agent_survey.status = self.status | |
544 | - self._agent_survey.save() | |
539 | + #with transaction.atomic(): | |
540 | + self._agent_survey = AgentSurvey.objects.get(name=self.name) | |
541 | + self._agent_survey.mode = self.mode | |
542 | + self._agent_survey.status = self.status | |
543 | + self._agent_survey.save() | |
545 | 544 | |
546 | 545 | |
547 | 546 | def simulator_send_command_to_myself(self): |
... | ... | @@ -743,7 +742,14 @@ class Agent: |
743 | 742 | |
744 | 743 | def specific_process(self, cmd:Command): |
745 | 744 | """ |
746 | - Sublevel Loop (only if ACTIVE) : | |
745 | + Sublevel Loop (only if ACTIVE) | |
746 | + | |
747 | + Dans le cas du Majordome, le specific_process() doit lancer | |
748 | + la prise d'une séquence. La sequence elle même va être une | |
749 | + boucle dans le specific_process. Donc on peut voir une séquence | |
750 | + comme un appel unique à une fonction qui va durer un certain | |
751 | + temps (max 20 min par exemple) mais dans la méthode | |
752 | + specific_process il y a une boucle sur la prise des images. | |
747 | 753 | """ |
748 | 754 | self.set_status(self.STATUS_SPECIFIC_PROCESS) |
749 | 755 | assert self.is_active() |
... | ... | @@ -825,11 +831,14 @@ class Agent: |
825 | 831 | multiprocessing.current_process().name, |
826 | 832 | threading.current_thread().name) |
827 | 833 | ) |
834 | + cmd.set_as_running() | |
835 | + """ | |
828 | 836 | if self.RUN_IN_THREAD: |
829 | 837 | cmd.set_as_running() |
830 | 838 | else: |
831 | 839 | with transaction.atomic(): |
832 | 840 | cmd.set_as_running() |
841 | + """ | |
833 | 842 | |
834 | 843 | # Default body of the specific processing |
835 | 844 | # Should be overriden by subclass |
... | ... | @@ -841,11 +850,14 @@ class Agent: |
841 | 850 | def thread_exec_specific_cmd_end(self): |
842 | 851 | """ specific command execution tearing down """ |
843 | 852 | cmd = self._current_specific_cmd |
853 | + cmd.set_as_processed() | |
854 | + """ | |
844 | 855 | if self.RUN_IN_THREAD: |
845 | 856 | cmd.set_as_processed() |
846 | 857 | else: |
847 | 858 | with transaction.atomic(): |
848 | 859 | cmd.set_as_processed() |
860 | + """ | |
849 | 861 | print(">>>>> Thread: ended execution of command", cmd.name) |
850 | 862 | cmd = None |
851 | 863 | # No more current thread | ... | ... |
src/agent/AgentX.py
... | ... | @@ -95,6 +95,8 @@ class AgentX(Agent): |
95 | 95 | def cmd_step1(self, step:int): |
96 | 96 | cmd = self._current_specific_cmd |
97 | 97 | cmd.result = f"in step #{step}/{self._thread_total_steps_number}" |
98 | + cmd.save() | |
99 | + """ | |
98 | 100 | if self.RUN_IN_THREAD: |
99 | 101 | print("(save from thread)") |
100 | 102 | cmd.save() |
... | ... | @@ -102,8 +104,10 @@ class AgentX(Agent): |
102 | 104 | #@transaction.atomic |
103 | 105 | print("(save from process)") |
104 | 106 | with transaction.atomic(): |
105 | - #Command.objects.select_for_update() | |
106 | 107 | cmd.save() |
108 | + #Command.objects.select_for_update() | |
109 | + """ | |
110 | + | |
107 | 111 | def cmd_step2(self, step:int): |
108 | 112 | self.cmd_step1(step) |
109 | 113 | def cmd_step3(self, step:int): |
... | ... | @@ -131,13 +135,23 @@ class AgentX(Agent): |
131 | 135 | # This is optional |
132 | 136 | self.thread_set_total_steps_number(5) |
133 | 137 | # HERE, write your own scenario |
134 | - self.thread_exec_specific_cmd_step(1, self.cmd_step1) | |
138 | + | |
139 | + # scenario OK | |
140 | + self.thread_exec_specific_cmd_step(1, self.cmd_step1, 1) | |
135 | 141 | self.thread_exec_specific_cmd_step(2, self.cmd_step2, 3) |
136 | 142 | self.thread_exec_specific_cmd_step(3, self.cmd_step1, 5) |
137 | 143 | self.thread_exec_specific_cmd_step(4, self.cmd_step3, 10) |
138 | 144 | self.thread_exec_specific_cmd_step(5, self.cmd_step1, 4) |
139 | 145 | # ... as many as you need |
140 | 146 | |
147 | + """ autre scenario | |
148 | + self.thread_exec_specific_cmd_step(1, self.cmd_step1, 1) | |
149 | + self.thread_exec_specific_cmd_step(2, self.cmd_step2, 2) | |
150 | + self.thread_exec_specific_cmd_step(3, self.cmd_step1, 2) | |
151 | + self.thread_exec_specific_cmd_step(4, self.cmd_step3, 2) | |
152 | + self.thread_exec_specific_cmd_step(5, self.cmd_step1, 3) | |
153 | + """ | |
154 | + | |
141 | 155 | ''' |
142 | 156 | # @override |
143 | 157 | def exec_specific_cmd_end(self, cmd:Command, from_thread=True): | ... | ... |