Python共享CPU时间? [英] Python share CPU time?

查看:56
本文介绍了Python共享CPU时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我想用Python编写一个小游戏,有点像robocode

http://robocode.sourceforge.net/)

问题是我必须在所有机器人之间共享CPU,

,从而为每个机器人分配一个时间段。但是我无法找到

任何方式来启动一个线程(机器人),并在给定时间后中断它

期间。

关于如何进行的任何建议?

Python是不适合这种事情的?


谢谢,

Yannick

Hi,

I would like to program a small game in Python, kind of like robocode
(http://robocode.sourceforge.net/).
Problem is that I would have to share the CPU between all the robots,
and thus allocate a time period to each robot. However I couldn''t find
any way to start a thread (robot), and interrupt it after a given time
period.
Any suggestions on how to proceed?
Is Python just not adapted to this kind of things?

Thanks,
Yannick

推荐答案

2006-08-09,Yannick< ya *************** @ gmail.comwrote:
On 2006-08-09, Yannick <ya***************@gmail.comwrote:




我想用Python编写一个小游戏,有点像robocode

http://robocode.sourceforge.net/)

问题是我必须在所有机器人之间共享CPU,

,从而为每个机器人分配一段时间。但是我无法找到

任何方式来启动一个线程(机器人),并在给定时间后中断它

期间。
Hi,

I would like to program a small game in Python, kind of like robocode
(http://robocode.sourceforge.net/).
Problem is that I would have to share the CPU between all the robots,
and thus allocate a time period to each robot. However I couldn''t find
any way to start a thread (robot), and interrupt it after a given time
period.



当机器人代码线程耗尽时需要阻止

要做。

The robot code thread needs to block when it runs out of things
to do.


有关如何进行的任何建议吗?
Any suggestions on how to proceed?



只需为每个机器人启动一个线程,然后在机器人的主循环中调用

time.sleep(0.010)码。调整

品尝0.010的价值。


作为睡觉的替代方案,你可以等待某种类型的

事件。通过循环的时间。

Just start a thread for each robot, and put a call to
time.sleep(0.010) in the main loop for the robot code. Adjust
the 0.010 value to taste.

As an alternative to sleeping, you could wait for some sort of
event each time through the loop.


Python是不适应这种事情的?
Is Python just not adapted to this kind of things?



它非常适合这类事情。


-

Grant Edwards grante哇!为什么不给你

永远进入和竞争,

visi.com马文?难道你不知道

你自己的ZIPCODE?

It''s quite well adapted to this sort of thing.

--
Grant Edwards grante Yow! Why don''t you
at ever enter and CONTESTS,
visi.com Marvin?? Don''t you know
your own ZIPCODE?


在Python中有几种方法可以实现并发。除了

线程模块之外,您是否尝试过FibraNet?它的设计考虑了简单的

游戏。

您可以在 http://cheeseshop.python.org/pypi/FibraNet

具体来说,FibraNet的nanothreads模块使用生成器

模拟轻量级合作线程。


基于生成器的方法快速且确定性*,不像真正的

线程,你需要比赛条件,等等。你可以暂停,恢复,终止或结束线程
。在Python 2.5中(很快就会发布
),生成器将成为消费者,而不仅仅是生成器,所以你可以将内容传递给生成器(不需要全局变量
作为一种解决方法)而不仅仅是能够即时吐出数据

。他们也会有一种更优雅的方式来处理异常。


有关这个概念的概述,特别是如果你想实现

这个你自己而不是使用Fibranet(虽然重复使用通常是更好的选择),请看看这个链接来自Charming Python

栏:
http://gnosis.cx/publish/programming...python_b7.html


如果这一切看起来太异国情调了,那么你也可以使用线程模块的

传统线程方法。通常

你会想要使用线程模块而不是较低级别的模块

" thread"模块。但要注意,线程是一大堆蠕虫。


我希望你觉得这很有用。


Yannick写道:
There''s several ways of doing concurrency in Python. Other than the
threading module, have you tried FibraNet? It''s designed with simple
games in mind.
You can download it at http://cheeseshop.python.org/pypi/FibraNet.
Specifically the nanothreads module from FibraNet uses generators to
simulate light-weight cooperative threads.

Generator-based approaches are fast and *deterministic*, unlike true
threads where you have to account for race conditions, etc. You can
pause, resume, kill, or end a "thread". In Python 2.5 (soon to be
released), generators will be consumers rather than just producers, so
you can pass things into generators (without needing global variables
as a workaround) rather than only being able to spit out data
on-the-fly. They''ll also have a more graceful way to handle exceptions.

For a good overview of the concept, especially if you want to implement
this yourself instead of using Fibranet (although re-use is often the
better choice), take a look at this link from the Charming Python
column:
http://gnosis.cx/publish/programming...python_b7.html.

If this all seems too exotic, then you can also just go with the
traditional threading approach with the threading module. Normally
you''ll want to use the "threading" module rather than the lower-level
"thread" module. But be warned, threads are a big can of worms.

I hope you find this useful.

Yannick wrote:




我想用Python编写一个小游戏,有点像robocode

http://robocode.sourceforge.net/)

问题是我必须在所有机器人之间共享CPU,

,从而为每个机器人分配一个时间段。但是我无法找到

任何方式来启动一个线程(机器人),并在给定时间后中断它

期间。

关于如何进行的任何建议?

Python是不适合这种事情的?


谢谢,

Yannick
Hi,

I would like to program a small game in Python, kind of like robocode
(http://robocode.sourceforge.net/).
Problem is that I would have to share the CPU between all the robots,
and thus allocate a time period to each robot. However I couldn''t find
any way to start a thread (robot), and interrupt it after a given time
period.
Any suggestions on how to proceed?
Is Python just not adapted to this kind of things?

Thanks,
Yannick


问题是我必须在所有机器人之间共享CPU,
Problem is that I would have to share the CPU between all the robots,

和从而为每个机器人分配一个时间段。但是我无法找到

任何方式来启动一个线程(机器人),并在给定时间后中断它

期间。

有关如何进行的任何建议?
and thus allocate a time period to each robot. However I couldn''t find
any way to start a thread (robot), and interrupt it after a given time
period.
Any suggestions on how to proceed?


>>导入线程,时间
def机器人(姓名):
>>import thread, time
def robot(name):



.... for x in xrange(5):

.. ..打印%s on pass%i %(姓名,i)

.... time.sleep(0.01)

....

.... for i in xrange(5):
.... print "%s on pass %i" % (name, i)
.... time.sleep(0.01)
....

>> ids = [thread.start_new(机器人,(机器人%i%i,i))i
中的i = class =post_quotes>

>> ids = [thread.start_new
>>ids = [thread.start_new(robot, ("Robot%i" % i,)) for i in



xrange(4)]

0号通行证上的Robot1

通过0时的Robot2

机器人0通过0

机器人3通过0

机器人1通过1

机器人0通过1

机器人2通过1

Robot3通过1

Robot1通过2

Robot0通过2

2通过机器人2

2号通行证上的Robot3

3号通行证上的机器人1

3号通行证上的Robot0

机器人2通过3

Robot3通过3

Robot1通过4

Robot0通过4

Robot2通过4

通过4上的Robot3

为每个机器人维护一系列事情可以让

机器人处理睡一会儿之前的一件事。


线程调度程序似乎确实提供了中断,因为我已经b / b
运行上面的代码而没有睡眠( )调用和更大的迭代,

你会发现它从机器人到机器人反弹。


或者,你可以利用python'的yield语句:

xrange(4)]
Robot1 on pass 0
Robot2 on pass 0
Robot0 on pass 0
Robot3 on pass 0
Robot1 on pass 1
Robot0 on pass 1
Robot2 on pass 1
Robot3 on pass 1
Robot1 on pass 2
Robot0 on pass 2
Robot2 on pass 2
Robot3 on pass 2
Robot1 on pass 3
Robot0 on pass 3
Robot2 on pass 3
Robot3 on pass 3
Robot1 on pass 4
Robot0 on pass 4
Robot2 on pass 4
Robot3 on pass 4

Maintaining a Queue of things to do for each robot allows the
robot to process a single thing before sleeping a bit.

The thread scheduler does seem to provide interruptions, as I''ve
run the above code with no sleep() call and larger iterations,
and you''ll find it bouncing from robot to robot.

Or, you can take advantage of python''s yield statement:


>> class Robot(object):
>>class Robot(object):



.... def __init __(self,name):

.... self.name = name

.... self.thought = 0

.... self.done = False

.... def think(self):

....而不是self.done:

....产生self.thought

.... self.thought + = 1

.... def完成(个体经营):

.... self.done = True
....

.... def __init__(self, name):
.... self.name = name
.... self.thought = 0
.... self.done = False
.... def think(self):
.... while not self.done:
.... yield self.thought
.... self.thought += 1
.... def finish(self):
.... self.done = True
....


>> robots = [Robot( str(i))for x in xrange(5)]
generators = [robot.think()for robot in robot in
[g.next()for g in generators]
>>robots = [Robot(str(i)) for i in xrange(5)]
generators = [robot.think() for robot in robots]
[g.next() for g in generators]



[0,0,0,0,0]

[0, 0, 0, 0, 0]


>> [g.next()for g in generators]
>>[g.next() for g in generators]



[1,1,1,1,1]

[1, 1, 1, 1, 1]


>> [g .next()for g in generators]
>>[g.next() for g in generators]



[2,2,2,2,2]

[2, 2, 2, 2, 2]


>> [g.next()for g in generators]
>>[g.next() for g in generators]



[3,3,3,3,3]

[3, 3, 3, 3, 3]


>> [生成器中g的g.next()]
>>[g.next() for g in generators]



[4,4,4,4,4 ]

[4, 4, 4, 4, 4]


>> generators [2] .next()
>>generators[2].next()



5

5


>> [g.next()for g in generators]
>>[g.next() for g in generators]



[5,5,6,5, 5]

[5, 5, 6, 5, 5]


>> robots [2] .thought
>>robots[2].thought



6

6


>>机器人[3] .thought
>>robots[3].thought



5


只需做更多think()方法中的复杂东西(对于大多数人来说,

计数并不是非常激动人心的思考)并且会产生你当前的状态。

5

Just do more complex stuff in the think() method (to most folks,
counting isn''t very exciting thinking) and yield your current state.


Python是不适应这种事情的?
Is Python just not adapted to this kind of things?



Python非常适合这些类型的东西......它需要一个适应性强的程序员才能充分利用它。 ..;)


-tkc


Python is quite adaptable to these sorts of things...it requires
an adaptable programmer to make the best use of it though... ;)

-tkc



这篇关于Python共享CPU时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆