简体   繁体   English

Python-如何同时运行两个循环?

[英]Python - How to run two loops at the same time?

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() 

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. 第一种解决方案是有一个独立的循环,该tick()在每次迭代时简单地调用每个对象的tick()方法。 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. 这些解决方案有时会带来相当大的复杂性,但好处是您可以让操作系统为每个对象安排tick()方法的运行。

我不明白您的代码在做什么,但是我的建议是使用线程: https : //pymotw.com/2/threading/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM