0

So simply put I want different objects to have their own tick loop code running independently of each other (As in one tick loop does not stop all other tick loops in my app).

I've created a basic module that has a class for shapes and the main body for spawning them however the tick loop of the class holds up the main parts loop.

I have even tried splitting the code into two modules to see if that would work but that still ran the two loops separately.

Here is my code:

(main code)

from random import *
from tkinter import *
from time import *

import RdmCirClass

size = 500
window = Tk()
count = 0
d = 0
shapes = []

canv = Canvas(window, width=size, height=size)
canv.pack()
window.update()
while True:
    col = choice(['#EAEA00'])
    x0 = randint(0, size)
    y0 = randint(0, size)
    #d = randint(0, size/5)
    d = (d + 0.01)
    outline = 'white'
    shapes.append(1)
    shapes[count] = RdmCirClass.Shape("shape" + str(count), canv, col, x0, y0, d, outline)
    shapes[count].spawn()
    count = count+1

    print("Circle Count: ",count)
    window.update()

(Shape Class)

from random import *
from tkinter import *
from time import *   

class Shape(object):
    def __init__(self,name, canv, col, x, y,d,outline):
        self.name = name
        self.canv = canv
        self.col = col
        self.x = x
        self.y = y
        self.d = d
        self.outline = outline
        self.age=0
        self.born = time()

    def death(self):
        pass

    def tick(self):
        self.age = time() - self.born

    def spawn(self):
        self.canv.create_oval(self.x, self.y, self.x + self.d, self.y + self.d, outline=self.outline, fill = self.col)
        while True:
            self.tick() 
3
  • If you need to interpolate two loops, consider converting one into a generator coroutine and then using yield passing between the coroutine and the main loop Commented Aug 22, 2016 at 2:49
  • By the end of this there will be posabley 50 different loops running. Im tryeing to recreate somthign thats similuer to how unity does there update() metheds in eatch of there class files Commented Aug 22, 2016 at 3:06
  • Then you definately want to consider something like I suggested. Thread proliferation helps nobody. See also the async module in P3 Commented Aug 22, 2016 at 5:17

2 Answers 2

1

Roughly speaking, there are three ways to accomplish what you want. Which is best depends greatly on what exactly you're trying to do with each independent unit, and what performance constraints and requirements you have.

The first solution is to have an independent loop that simply calls the tick() method of each object on each iteration. Conceptually this is perhaps the simplest to implement.

The other two solutions involve multiple threads, or multiple processes. These solutions come with sometimes considerably complexity, but the benefit is that you get the OS to schedule the running of the tick() method for each object.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok so I think it's going way more complacated then what it realey is. Basicley what I want is a function that gets called on evey tick. I whant to have mutaple instances of this function running at the same time. There's a undefind number of instances of this function. Also depending on what class this function is in it could be compleatley different. The Onley thing this function has in commen is its name and that it gets called evey tick. That's it
Perhaps you need to think a bit more architecturally then. Create a super class or mix-in class that defines a tick function and a function to evaluate if its finished ticking, subclass these with the tick containing the code present in the loop, and whatever the exit condition is, and finally have an iterator that knows how to drive the looped classes
0

I don't understand what your code is doing, but my suggestion is to use threading: https://pymotw.com/2/threading/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.