开发者

Python-brisa works in Eclipse but not in the shell

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()


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜