Pythonbrisa在Eclipse中工作,但在sh中不工作

2024-05-15 04:59:03 发布

您现在位置:Python中文网/ 问答频道 /正文

下一个python-brisa代码在Eclipse中工作,但是如果我从shell执行它,它就会被卡住。我认为问题出在主反应堆(). 因为如果我对它进行注释并创建一个无限循环,那么程序将在Eclipse和shell中工作。你知道我该怎么修吗?你知道吗

python版本是2.6.6,我正在使用Debian测试(wheezy)。你知道吗

#!/usr/bin/env python

from brisa.core.reactors import install_default_reactor
from brisa.core.threaded_call import run_async_function

reactor = install_default_reactor()

import thread
import sys

from brisa.upnp.control_point.control_point import ControlPoint

class CommandLineCtrlPoint(ControlPoint):

    def __init__(self):
        ControlPoint.__init__(self)
        self.running = False
        self.commands = {'option1': 'option1',
                         'option2': 'option2',
                         'option3' :'option3',
                         'help': self._help}

    def run(self):
        try:
            self.running = True
            reactor.add_after_stop_func(self.stop)
            thread.start_new_thread(self._handle_cmds,())
            reactor.main()
#            while(True):
#                pass
        except Exception, e:
            print e

    def _help(self):
        help = 'commands: '
        for k in self.commands.keys():
          help += k + ', '
        print help[:-2]

    def _handle_cmds(self):
       try:
           while self.running:
               command = str(raw_input('>>>'))
               try:
                   print command
                   self.commands[command]()
               except KeyError:
                   print 'invalid command, try help'
               command = ''
       except Exception, e:
            print e

def main():
    print "Test Program\n"
    cmdline = CommandLineCtrlPoint()
    cmdline.run()

if __name__ == "__main__":
    main()

Tags: runfromimportselfmaindefhelpthread
1条回答
网友
1楼 · 发布于 2024-05-15 04:59:03

我发现了“错误”。解决方案是替换线路:

command = str(raw_input('>>>'))

print '>>> ',
command = sys.stdin.readline().replace('\n','')

我不确定,但原因可能是这样的:
http://pydev.org/faq.html#why_raw_input_input_does_not_work_correctly

如果有人有其他解释,我将不胜感激,如果评论它。你知道吗

相关问题 更多 >

    热门问题