Evolife
Evolife has been developed to study Genetic algorithms, Natural evolution and behavioural ecology.
Simulation_Thread.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2""" @brief Simulation thread. """
3
4#============================================================================#
5# EVOLIFE http://evolife.telecom-paris.fr Jean-Louis Dessalles #
6# Telecom Paris 2022-04-11 www.dessalles.fr #
7# -------------------------------------------------------------------------- #
8# License: Creative Commons BY-NC-SA #
9#============================================================================#
10# Documentation: https://evolife.telecom-paris.fr/Classes #
11#============================================================================#
12
13
14
17
18
19
20from sys import excepthook, exc_info
21from threading import Thread
22from time import sleep
23
24
25
28
29class Simulation(Thread):
30 """ Thread triggered by the "Step" and "Run" buttons
31 of the Evolife window system
32 """
33 def __init__(self, OneStep, funcmode, ReturnFromThread):
34 Thread.__init__(self)
35 self.Back = ReturnFromThread
36 self.Continuous = funcmode
37 self.Running = False
38 self.BusyDisplay = False # indicates that display needs time to update
39 self.OneStep = OneStep # function that runs one simulation step
40
41 def stop(self):
42 """ stops the thread """
43 if self.Running:
44 self.Running = False
45 if self.BusyDisplay:
46 while self.Back('Busy?'):
47 sleep(0.1)
48 # print "stopping the process"
49 self.join()
50
51 def run(self):
52 """ launched by start() """
53 self.Running = True
54 while self.Running:
55 try:
56 ReturnFromDisplay = self.Back(self.OneStep())
57 if ReturnFromDisplay < 0:
58 # We have to stop
59 self.running = False
60 break
61 elif ReturnFromDisplay > 0:
62 # Display is jammed - We have to wait
63 self.BusyDisplay = True
64 while self.Back('Busy?'):
65 sleep(0.1)
66 self.BusyDisplay = False
67 except:
68 self.running = False
69 excepthook(exc_info()[0],exc_info()[1],exc_info()[2])
70 break
71 if not self.Continuous:
72 break
73
74
75
76
79
80if __name__ == "__main__":
81 print(__doc__ + '\n')
82 print(Simulation.__doc__ + '\n')
83 raw_input('[Return]')
84
85
86__author__ = 'Dessalles'