Python - 线程、计时或函数使用?
我在制定如何解决这个问题的想法时遇到问题。请帮忙。
我的项目由一个 N x N 网格组成,其中包含一系列块,这些块应该在该网格内以随机方向和随机速度移动(每 0.1 秒,块的位置随速度更新)。我有三个“特殊”块,预计它们具有单独的运动功能。我会让其他块(其中很多)除了更新它们的位置并确保它们保留在网格中之外什么都不做。
现在这三个块具有移动之外的功能,但是每个块都单独运行,等待另一个块的特殊功能完成(块2将等待块1,块3将等待2并将其设置回块1,等等。 )这种队列将在运动发生时运行。我希望运动永远不会停止。每个块的非移动函数运行n次后,代码结束。
我的问题是:我应该使用线程来启动和停止非移动函数,还是有办法只设置一个时间并设置布尔值,以便在 0.1 秒后使用类函数来连续移动对象(显然一遍又一遍地循环),然后使用计数来一起结束程序?如果是这样,你会如何在Python中为此编写main函数?对于所有这些发生的情况,是否有人认为 Java 的运行速度会比 Python 快得多,尤其是将数据写入 .txt 文件时?
I am having an issue formulating an idea on how to work this problem. Please help.
My project consists of an N x N grid with a series of blocks that are supposed to move in a random direction and random velocity within this grid (every .1 seconds, the location of the block is updated with the velocity). I have three "special" blocks that are expected to have individual movement functions. I will have other blocks (many of them) doing nothing but updating their location, and making sure they remain in the grid.
Now these three blocks have functions beyond movement, but each of these runs individually, waiting for the other block's special function to finish (block 2 will wait on block 1, Block 3 will wait on 2 and set it back to block 1, etc.) This queue of sorts will be running while the motion is happening. I want the motion to never stop. After each block's non-movement function runs n times, the code finishes.
My question is this: should I use threads to start and stop the non-movement functions, or is there a way to just set a time and set booleans that could use a class function after .1 seconds to continuously move the objects (and obviously loop over and over), and then use counts to end the program all together? If so, how would you write main function for this in Python? For all of this happening, does anyone think that Java would be significantly faster than Python in running this, especially if writing the data to a .txt file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您最好的选择可能是在单个更新函数中一次处理所有这些,而不是尝试使用线程。这主要是因为全局解释器锁无论如何都会阻止多个线程同时处理。然后你要做的是这样的:
然后我们定义第二个函数,它将无限期地运行我们的第一个函数:
现在我们有了一个间隔或排序,我们将启动一个在后台运行并处理我们的显示更新的线程。
在我们的
tick()
函数中,我们满足特殊块 1、2 和 3 按设定顺序工作的要求。其他盒子都会采取自己的行动,而不管其他盒子做什么。Your best bet is probably to handle all of them at once in a single update function rather than attempting to use Threads. This is primarily because the Global Interpreter Lock will prevent multiple threads from processing concurrently anyway. What you're after then is something like this:
Then we define a second function that will run our first function indefinitely:
Now that we have an interval or sorts, we'll launch a Thread that runs in the background and handles our display updates.
In our
tick()
function we've worked in the requirements that specialBlocks 1, 2, and 3 are working in a set order. The other boxes each take their actions regardless of what the others do.如果将对特殊函数的调用放在一个函数中,您将获得协调 ( 2)免费。
至于Python 与Java 的速度,这取决于很多因素,比如具体的实现。能说的信息太少了。
If you put the calls to the special functions together in a single function, you get coordination (2) for free.
As for the speed of Python versus Java, it depends on many things, such as the exact implementation. There is too little information to say.