为什么这段代码会崩溃python? [英] Why does this code crash python?

查看:68
本文介绍了为什么这段代码会崩溃python?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个程序,它将基本上模拟一个国际象棋在python中的
时钟。要做到这一点,我有两个线程在运行,一个是

更新当前运行的时钟,另一个是监视

按键。我正在使用effbot控制台模块,这就是我获得按键事件的事件

。但是当我按下太空时,不久之后就崩溃了。

。程序停止,控制台关闭,窗口说

程序pythonw.exe有意外关闭的原因。
我使用的是python2.5。 br />

如果有一种更简单的方法可以做到这一点(我试过没有线程,但是有一些问题,控制台模块的一些问题让我输入正确但仍然是

让计时器显示倒计时)我想知道它。我是
我不是一个非常优秀的程序员,并且没有太多的经验

python


注:国际象棋时钟是两个从设定时间开始倒计时的计时器,当你启动其中一个计时器时,它就是



以下代码:


导入时间

导入控制台

来自线程导入线程


c = Console.getconsole( )


转= 0

班级计时器(线程):

def run(self):

全球转向

oldTime = [time.time(),time.time()]

timeLeft = [260,260]


go = True

go:

newTime = time.time()

timeLeft [转] - = newTime - oldTime [转]

oldTime [转] =新时间

分钟= [str(int(timeLeft [0] // 60)),

str(int(timeLeft [1] // 60))]

秒= [str(timeLeft [0]%60)[0:5],

str(timeLeft [1]%60)[0:5]]


if float(秒[0] )< 10:秒[0] =''0''+

秒[0] [: - 1]

浮点数(秒[1])< 10:秒[1] =''0''+

秒[1] [: - 1]


c.text(3,3,分钟[0] +'':''+秒[0])

c.text(12,3,分钟[1] +'':''+秒[1])


time.sleep(.1)


class eventMonitor(Thread):

def run(self):

全球回合

go = True

go go:

event = c.peek()

if event!= None:

c.get()

if event.type ==''KeyPress'':

如果event.keycode == 32:

if turn == 1:turn = 0

if turn == 0:turn = 1

c.text(10,20,''1'')


timer()。start()

eventMonitor()。start()

I am trying to make a program that will basically simulate a chess
clock in python. To do this I have two threads running, one that
updates the currently running clock, and one that watches for a
keypress. I am using the effbot Console module, and that is where I get
the events for the keypresses. But when I press space it crashes
shortly after. The program stops, the Console closes, and windows says
that the program pythonw.exe has had an unexpected reason to shut down.
I am using python2.5.

If there is a simpler way to do this ( I tried without threads but had
some problems with the Console module giving me input right and still
letting the timer display count down ) I would like to know about it. I
am not a very good programmer, and don''t have much experience with
python

note: A chess clock is two timers that count down from a set time, and
when you start one of the timer the other stops.

Code below:

import time
import Console
from threading import Thread

c = Console.getconsole()

turn = 0

class timer (Thread):
def run ( self ):
global turn
oldTime = [time.time(), time.time()]
timeLeft = [260, 260]

go = True
while go:
newTime = time.time()
timeLeft[turn] -= newTime - oldTime[turn]
oldTime[turn] = newTime
minutes = [str(int(timeLeft[0]//60)),
str(int(timeLeft[1]//60))]
seconds = [str(timeLeft[0]%60)[0:5],
str(timeLeft[1]%60)[0:5]]

if float(seconds[0]) < 10: seconds[0] = ''0'' +
seconds[0][:-1]
if float(seconds[1]) < 10: seconds[1] = ''0'' +
seconds[1][:-1]

c.text(3,3,minutes[0] + '':'' + seconds[0])
c.text(12,3,minutes[1] + '':'' + seconds[1])

time.sleep(.1)

class eventMonitor (Thread):
def run ( self ):
global turn
go = True
while go:
event = c.peek()
if event != None:
c.get()
if event.type == ''KeyPress'':
if event.keycode == 32:
if turn == 1: turn = 0
if turn == 0: turn = 1
c.text(10,20,''1'')

timer().start()
eventMonitor().start()

推荐答案

我的***** @ gmail.com 写道:

我正在尝试制作一个程序,它将基本上模拟一个国际象棋在python中的
时钟。要做到这一点,我有两个线程在运行,一个是

更新当前运行的时钟,另一个是监视

按键。我正在使用effbot控制台模块,这就是我获得按键事件的事件

。但是当我按下太空时,不久之后就崩溃了。

。程序停止,控制台关闭,窗口说

程序pythonw.exe有意外关闭的原因。
我正在使用python2.5。
I am trying to make a program that will basically simulate a chess
clock in python. To do this I have two threads running, one that
updates the currently running clock, and one that watches for a
keypress. I am using the effbot Console module, and that is where I get
the events for the keypresses. But when I press space it crashes
shortly after. The program stops, the Console closes, and windows says
that the program pythonw.exe has had an unexpected reason to shut down.
I am using python2.5.



尝试不(重新)使用在另一个线程中的某个线程中创建的对象。

您正在创建一个控制台对象 ; C"并在两个线程中使用它。


尝试自己为每个线程创建控制台对象,

即删除全局c = Console.getconsole( )"并在每个线程类中用一个getconsole()取代




我没有Console模块所以我不知道是否它为你修好了东西

,但试一试:)


--Irmen

Try not (re)using an object created in a certain thread in another thread.
You''re creating a console object "c" and use it in both threads.

Try creating the console object for each thread by itself,
i.e. remove the global "c = Console.getconsole()" and replace
it by a getconsole() inside each thread class.

I don''t have the Console module so I don''t know if it fixes things
for you, but give it a try :)

--Irmen


11月11日上午11点28分,Irmen de Jong< irmen.NOS ... @ xs4all.nlwrote:
On Nov 11, 11:28 am, Irmen de Jong <irmen.NOS...@xs4all.nlwrote:

你正在创建一个控制台对象" C"并在两个线程中使用它。


尝试自己为每个线程创建控制台对象,

即删除全局c = Console.getconsole( )"并在每个线程类中用一个getconsole()取代




我没有Console模块所以我不知道是否它为你修复了一些东西

,但试一试:)
You''re creating a console object "c" and use it in both threads.

Try creating the console object for each thread by itself,
i.e. remove the global "c = Console.getconsole()" and replace
it by a getconsole() inside each thread class.

I don''t have the Console module so I don''t know if it fixes things
for you, but give it a try :)



嗯,我试过了,它做了一些事情。它让空格键

切换了一次,但不是第二次。而且它仍然崩溃了,但是并不总是在同一时间,有时它会在第二次

的时候撞到空格键,有时候在我第一次击中它之前时间,但是当我做了一些会产生控制台事件的事情(移动

鼠标或其他什么东西)时,我总是花b $ b。所以,我认为它与

控制台无法正常运行,所以我把它完全取出了

个线程并且只运行了事件检查循环本身,它还是b $ b仍然崩溃了。显然我使用Console事件是错误的。我是试图在网上找一些关于如何观看活动的例子

,但如果情况变得更糟,我可以尝试使用键盘挂钩,

我之前看过那些,他们可能会更好。


- Mythmon

Well, I tried that, and it did something. It made it so the space bar
switched the clock once, but not a second time. And it still crashed,
but not always at the same time, sometimes it would do it the second
time I hit the space bar, sometimes before I hit it the first time, but
always when i did something that would generate a Console event (moving
the mouse or something). So, I thought it had something to do with
Console not working well with threads, so I took it completely out of
threads and had just the event checking loop run by itself, and it
still crashed. So apparently I am using the Console events wrong. I am
going to try and find some examples online on how to watch for events
with it, but if worse comes to worse I can try and use keyboard hooks,
I''ve looked at those before, and they might work better.

--Mythmon

< br>

在文章< 11 ********************** @ h54g2000cwb.googlegroups .com> ;,
我的***** @ gmail.com 写道
In article <11**********************@h54g2000cwb.googlegroups .com>,
My*****@gmail.com wrote

>好吧,我试过了,它做了一些事情。它让空格键打开时钟一次,但不是第二次。它仍然会崩溃,但并非总是在同一时间,有时它会在我第二次打到空格键时这样做,有时候在我第一次碰到它之前,但是总是当我做了一些会产生控制台事件(移动鼠标或其他东西)的东西。所以,我认为这与
控制台与线程无法正常工作有关,所以我把它完全从
线程中拿出来,并且只有事件检查循环自己运行,并且它
仍然坠毁。显然我使用Console事件是错误的。我试着在网上找一些关于如何用它来观看事件的例子,但如果情况变得更糟,我可以尝试使用键盘钩,
我看了在那之前,他们可能会更好地工作。
>Well, I tried that, and it did something. It made it so the space bar
switched the clock once, but not a second time. And it still crashed,
but not always at the same time, sometimes it would do it the second
time I hit the space bar, sometimes before I hit it the first time, but
always when i did something that would generate a Console event (moving
the mouse or something). So, I thought it had something to do with
Console not working well with threads, so I took it completely out of
threads and had just the event checking loop run by itself, and it
still crashed. So apparently I am using the Console events wrong. I am
going to try and find some examples online on how to watch for events
with it, but if worse comes to worse I can try and use keyboard hooks,
I''ve looked at those before, and they might work better.



亲爱的Pythonauts,


我经常潜伏在comp.lang.python新闻组。我不是一个专家,但是我有一种越来越多的感觉,那就是在Python的并发性方面肯定缺乏的东西。它是
与Java相同的问题。请原谅我,如果我不知道正确的词汇,那么Python并发性似乎也暴露在低a />
级别。它就像汇编程序一样:它实现了所有_possible_,但是在实践中它是非常复杂和脆弱的,小故障的,并且真的很难以b $ b难以实现延伸。就像Dartmouth BASIC中的编程一样只需要

条件和goto结构化编程之前的说明。


更高级别的并发系统,不是基于监视器和

锁和伟大的程序员纪律,最终需要制作

" Python 3000"现实。


与此同时,是否有任何地方或任何事情讨论了与Python相关的各种并发选项?还有Stackless Python

(我无法做出头或尾;我一直无法找到任何

清晰的概述,或者真正的解释设计的目的。)我知道有一个针对Python的Erlang系统的软件包,某处

(可能是Parnassus)。某处可能还有Py-CSP。很多

的树木,但木材在哪里?


比较和讨论并发/分布式模型在哪里?


亲切的问候,


Sandy

-

Alexander Anderson< ju ****** ****@alma-services.abel.co.uk>

(英格兰约克郡)


如果没有异象,人们就会灭亡。

Dear Pythonauts,

I usually lurk on the comp.lang.python newsgroup. I''m not an expert
in the slightest, but I have had a growing feeling that there''s
something definitely lacking in the concurrency aspects of Python. It
is the same problem with Java. Forgive me if I don''t know the right
vocabulary, but Python concurrency seems to be exposed at too "low a
level". It''s like assembler: it''s all _possible_ to implement, but in
practise it''s very complicated and fragile, glitchy, and really
difficult to extend. Like programming in Dartmouth BASIC with just
conditionals and "goto" instructions, before structured programming.

A higher-level system of concurrency, not based on monitors and
locks and great programmer discipline, will ultimately require making
"Python 3000" a reality.

In the meantime, is there anywhere, or any thing, that discusses the
various concurrency options related to Python? There''s Stackless Python
(which I can''t make head or tail of; I have been unable to find any
lucid overview, or genuine explanation of the purpose of the design.) I
know that there''s a package for an Erlang system for Python, somewhere
("Parnassus" probably). There''s probably a Py-CSP somewhere too. Lots
of trees, but where''s the Wood?

Where are concurrency/distributed models compared and discussed?

With kind regards,

Sandy
--
Alexander Anderson <ju**********@alma-services.abel.co.uk>
(Yorkshire, England)

Where there is no vision, the people perish.


这篇关于为什么这段代码会崩溃python?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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