'How do I make an action happen only once per n steps?
I tried using the steps variable, but it didn't get any results. Is there a way to make the addition of the asteroids so that it happens once per n steps?
asteroids = Group()
spaceship = RegularPolygon(200, 350, 15, 3, fill='white')
lasers = Group()
counter = Label(0, 380, 20, size=20, bold=True, fill='white')
def onMouseMove(mouseX, mouseY):
spaceship.centerX = mouseX
def onMousePress(mouseX, mouseY):
lasers.add(Rect(mouseX, 320, 2, 15, fill='lime'))
def onStep():
asteroids.add(Circle(randrange(0, 400), 0, 30, fill='gray'))
for laser in lasers:
laser.centerY = laser.centerY-30
if (laser.centerY < 5):
lasers.remove(laser)
for asteroid in asteroids:
asteroid.centerY = asteroid.centerY+3
if (asteroid.centerY > 399):
asteroids.remove(asteroid)
Solution 1:[1]
Use enumerate to determine whether the number of iterations is divided by n with no remainder.
Note that you should not modify the original container during traversal. Traversing the copy of the container is a good habit:
n = 5 # According to your situation.
for i, laser in enumerate(lasers.copy(), 1):
laser.centerY -= 30
if i % n == 0 and laser.centerY < 5:
lasers.remove(laser)
Solution 2:[2]
Is this close to what you are trying to do? There is a counter that decrements until zero. At zero, do the update and reset the counter.
asteroid_update = 0
asteroids = Group()
spaceship = RegularPolygon(200, 350, 15, 3, fill='white')
lasers = Group()
counter = Label(0, 380, 20, size=20, bold=True, fill='white')
def onMouseMove(mouseX, mouseY):
spaceship.centerX = mouseX
def onMousePress(mouseX, mouseY):
lasers.add(Rect(mouseX, 320, 2, 15, fill='lime'))
def onStep():
global asteroid_update
# I don't know how the next line fits into your 5th step or every stop logic
asteroids.add(Circle(randrange(0, 400), 0, 30, fill='gray'))
for laser in lasers:
laser.centerY = laser.centerY-30
if (laser.centerY < 5):
lasers.remove(laser)
if asteroid_update:
asteroid_update -= 1
else:
asteroid_update = 5 # reset counter
for asteroid in asteroids:
asteroid.centerY = asteroid.centerY+3
if (asteroid.centerY > 399):
asteroids.remove(asteroid)
Solution 3:[3]
Solve Kerberos issue, instead of adding work arounds. I'm not sure how you are using the kerberos principal, but I will point out that the documentation maintains a solution for this issue:
Long-Running
Applications Long-running applications may run into issues if their run time exceeds the maximum delegation token lifetime configured in services it needs to access.
This feature is not available everywhere. In particular, it’s only implemented on YARN and Kubernetes (both client and cluster modes), and on Mesos when using client mode.
Spark supports automatically creating new tokens for these applications. There are two ways to enable this functionality.
Using a Keytab
By providing Spark with a principal and keytab (e.g. using spark-submit with --principal and --keytab parameters), the application will maintain a valid Kerberos login that can be used to retrieve delegation tokens indefinitely.
Note that when using a keytab in cluster mode, it will be copied over to the machine running the Spark driver. In the case of YARN, this means using HDFS as a staging area for the keytab, so it’s strongly recommended that both YARN and HDFS be secured with encryption, at least.
I would also point out that caching will reduce visits to HDFS but may still require reads from HDFS if there isn't sufficient space in memory. If you don't solve the Kerberos issue because of [reasons]. You may wish to instead use checkpoints. They are slower than caching, but are made specifically to help [long running process that sometimes fail] get over that hurdle of expensive recalculation, but they do require disk to be written to. This will remove any need to revisit the original HDFS cluster. Typically they're used in Streaming to remove data lineage, but they also have their place in expensive long running spark applications. (You also need to manage their cleanup.)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | RufusVS |
| Solution 3 | Matt Andruff |
