Need help Combining time.sleep with multiprocessing in python 3

Creating a corner-based game using python 3. I want 2 characters (enemy and enemy) to attack, stop at random + speed, and then attack again if they are still alive.

The problem I am facing is time.sleep, which freezes both modules, not one or the other. Any suggestions for working efficiently?

from multiprocessing import Process import time import random def timing1(): speed=60#SPEED IS NORMALLY A KEY FROM LIST, USING 60 FOR EXAMPLE sleeptime=36/((random.randint(1,20)+speed)/5) print (sleeptime) time.sleep(sleeptime) input('HERO ACTION') def timing2(): speed=45 sleeptime=36/((random.randint(1,20)+speed)/5) print (sleeptime) time.sleep(sleeptime) input('FOE ACTION') if __name__ == '__main__': p1=Process(target=timing1) p1.start() p2=Process(target=timing2) p2.start() p1.join() p2.join() 
+4
source share
2 answers

After one move, processes exit your code. Your main process expects both processes to exit by calling .join() on them, i.e. time.sleep() will not freeze other processes.

Add while True: to the top of the timing*() functions to make several moves.

Here you do not need several processes. You can use threads or even a single thread to make turns.

0
source

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__": # Call when the reactor has started. reactor.callWhenRunning(heroStart) reactor.callWhenRunning(foeStart) reactor.run() 

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.

0
source

Source: https://habr.com/ru/post/1444499/


All Articles