Python中的初始化问题

时间:2011-02-17 00:52:57

标签: python

错误在最后给出:

from brisa.core.reactors import install_default_reactor   
reactor = install_default_reactor()    
print reactor    

from brisa.upnp.control_point.control_point import ControlPoint    
def on_new_device(dev):        

    print 'Got new device:', dev.udn    
    print "Type 'list' to see the whole list"    
    if not dev:    
        return

def create_control_point():   
    c = ControlPoint()    
    print "hello"       
    c.subscribe('new_device_event', on_new_device)      
    print "c"    
    return c    

def main():    
    print "Inside main"             
    c = create_control_point()    
    print "Inside create control point"
    c.start()    
    reactor.add_after_stop_func(c.stop)    
    reactor.main()

def _search(c):    
    """ Start searching for devices of type upnp:rootdevice and repeat    
    search every 600 seconds (UPnP default)    
    """    
    c.start_search(600, 'upnp:rootdevice')    

def _stop(c):    
    """ Stop searching    
    """    
    c.stop_search()     

def _list_devices(c):    
    """ Lists the devices that are in network.    
    """    
    k = 0    
    for d in c.get_devices().values():    
        print 'Device no.:', k    
        print 'UDN:', d.udn    
        print 'Name:', d.friendly_name    
        print 'Device type:', d.device_type    
        print 'Services:', d.services.keys() # Only print services name    
        print
        k += 1

# Control the loop at _handle_cmds function
running_handle_cmds = True
commands = {             
            'search': _search,
            'stop': _stop,
            'list': _list_devices,            
            }

def _handle_cmds(c):
    while running_handle_cmds:
        try:
            input = raw_input('>>> ').strip()
            if len(input.split(" ")) > 0:
                try:
                    if len(input.split(" ")) > 1:
                        commands[input.split(" ")[0]](c, input.split(" ")[1])
                    else:
                        commands[input.split(" ")[0]](c)
                except KeyError, IndexError:
                    print 'Invalid command, try help'
                except TypeError:
                    print 'Wrong usage, try help to see'
        except KeyboardInterrupt, EOFError:
            c.stop()
            break

    # Stops the main loop
    reactor.main_quit()

if __name__ == '__main__':    
    print "hello"    
    main()

错误:

ankit@ubuntu:~/Desktop$ python controlpt.py
<brisa.core.reactors.glib2.GLib2Reactor object at 0x965bdcc>
hello
Inside main

之后我知道,它不是初始化控制点。有关库(控制点)的信息可在http://brisa.garage.maemo.org/apidoc/index.html

处获得

1 个答案:

答案 0 :(得分:0)

如果这与其他一些基于事件的库(如Twisted)类似,那么您希望在执行其他任何操作之前让反应堆的主要运行。

在主要功能中放弃对reactor.main的呼叫。然后,不要只在底部的main中调用if,而是执行此操作:

if __name__ == "__main__":
    run_async_function(main)
    reactor.main()

看看事情是否表现得更像你期望的那样。

run_async_function位于threaded_call模块中。)