Python-brisa 可在 Eclipse 中运行,但不能在 shell 中运行

发布于 2024-11-15 20:01:30 字数 1752 浏览 3 评论 0原文

下一个 python-brisa 代码可以在 Eclipse 中运行,但如果我从 shell 执行它,就会卡住。我认为问题出在reactor.main()中。因为如果我注释它并创建一个无限循环,该程序将在 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()

The next python-brisa code works in Eclipse but gets stuck if I execute it from the shell. I think that the problem is in reactor.main(). Because if I comment it and I create a infinite loop the program works in Eclipse and in the shell. Any idea of how can I fix it?

The python version is the 2.6.6 and I am using Debian Testing (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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情归归情 2024-11-22 20:01:30

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

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

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

不确定,但也许原因可以在这个解释中:
如果有人有其他

解释,我将不胜感激如果评论的话。

I found the "error". The solution was substitute the line:

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

by

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

I am not sure but maybe the reason of this can be in this explanation:
http://pydev.org/faq.html#why_raw_input_input_does_not_work_correctly

If someone has another explanation I will grateful if comment it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文