Ant simulation: it's better to create a Process/Thread for each Ant or something else?
The simple study is: Ant life simulation
I'm creating an OO structure that see a Class for the Anthill, a Class for the Ant and a Class for the whole simulator.
Now I'm brainstorming on "how to" make Ants 'live'...
I know that there are projects like this just started but I'm brainstorming, I'm not looking for a just-ready-to-eat-dish.
Sincerely I have to make some tests for understand on "what is better", AFAIK Threads, in Python, use less memory than Processes.
What "Ants" have to do when you start the simulation is just: moving around with random direction, if they found food ->eat/bring to the anthill, if they found another ant from another anthill that is transporting food -> attack -> collect food -> do what have to do.... and so on...that means that I have to "share" information across ants and across the whole enviroment.
开发者_如何学Pythonso I rewrite: It's better to create a Process/Thread for each Ant or something else?
EDIT: In cause of my question "what is better", I'd upvoted all the smart answers that I received, and I also put a comment on them. After my tests, I'll accept the best answer.
I would recommend to have a look at stackless. Stackless introduces tasklets which are akind of microthreads which allows to get the benefits of thread-based programming without the performance and complexity problems associated with conventional threads
A possible problem with stackless is, that as far as i know you need to use a modified interpreter or pypy to use the microthreads. However it might be worth it, as there are a some companies that use stackless with great success (eg. for it is used for EVE Online)
Also have a look at greenlet which also offers you a kind of microthreads, without replacing the interpreter. However compared to stackless greenlet only offers a limited featureset.
If you don't mind GPL, I suggest you use the Khronos simulation framework, which allows you to define each ant as a generator so you don't need to create threads. The Khronos engine takes care of the scheduling.
I'm actually developing a competing project called GarlicSim which you can also use for your simulation, but for your case Khronos would be better. (Unless you have a problem with GPL.)
I wrote an ant simulation (for finding a good TSP-solution) and a wouldnt recommend a Thread-Solution. I use a loop to calculate for each ant the next step, so my ants do not really behave concurrently (but synchronize after each step).
I don't see any reason to model those ants with Threads. Its no advantage in terms of run-time behavior nor is it an advantage in terms of elegancy (of the code)!
It might be, admittedly, slightly more realistic to use Threads since real ants are concurrent, but for simulations purposes this is IMHO neglectable.
I agree with @delan - it seems like overkill to allocate a whole thread per Ant, especially if you are looking to scale this to a whole anthill with thousands of the critters running around.
Instead you might consider using a thread to update many ants in a single "cycle". Depending on how you write it - you need to carefully consider what data needs to be shared - you might even be able to use a pool of these threads to scale up your simulation.
Also keep in mind that in CPython the GIL prevents multiple native threads from executing code at the same time.
I think the thread solution is the best. Even if ants are individuals, they share the environment, exactly what is permitted when using thread. Process solution corresponds to the reality, it would be arduous to implements communication system with the environment.
Another solution would be to define that ants act only when a tick happen. Then, there is no need to use thread or process.
For example :
import time
...
while True:
for ant in ants:
ant.act()
time.sleep(tickwait)
Meanwhile, this solution is easier to implement.
精彩评论