Develop jf
My general advice on streaming presentation is to introduce it if absolutely necessary.
- You literally block low-level I / O, and there are no alternatives besides using your own threads.
- You fall within the bounds of computation and must use more cores, in which case python because of it the GIL may work against you anyway.
Alternatively, use a library that the scheduler provides, such as twisted or gevent , which does not rely on its own threads to schedule events.
Gevent
You can write your game in a manner with a well-thought-out model, but do not worry about resource competition between threads. You should keep in mind to use the gevent version of various functions such as sleep in your example.
import random import gevent def hero(): speed = 60 sleeptime = 36 / ((random.randint(1, 20) + speed) / 5) print (sleeptime) gevent.sleep(sleeptime) input('HERO ACTION') def foe(): speed = 45 sleeptime = 36 / ((random.randint(1, 20) + speed) / 5) print (sleeptime) gevent.sleep(sleeptime) input('FOE ACTION') if __name__ == "__main__": heroThread = gevent.Greenlet(hero) foeThread = gevent.Greenlet(foe) heroThread.start() foeThread.start() gevent.joinall([heroThread, foeThread])
twisted
It supplies an event reactor that is written in pure python and does not purport to be anything larger or smaller than a single-threaded event reactor (aka Event Loop ). This would require more enumeration of your example.
import random from twisted.internet import reactor def heroAction(): input('HERO ACTION') def heroStart(): speed = 60 sleeptime = 36 / ((random.randint(1, 20) + speed) / 5) print (sleeptime) reactor.callLater(sleeptime, heroAction) def foeAction(): input('FOE ACTION') def foeStart(): speed = 45 sleeptime = 36 / ((random.randint(1, 20) + speed) / 5) print (sleeptime) reactor.callLater(sleeptime, foeAction) if __name__ == "__main__":
Note that a twisted reactor will not turn off when it has nothing to do, this is clearly left to the programmer.
Roller performance
For training purposes, it may be interesting to write your own planner, or you may have requirements in your game, such as justice, that are required. A good starting point would be to look at another minimalistic planner for inspiration.
source share