简体   繁体   English

调用构造函数后,Python退出

[英]Python quits after a constructor is called

My code seems to be failing after the constructor is called. 调用构造函数后,我的代码似乎失败。
Here is my code - it is failing on the c = create_control_point() line in main() : 这是我的代码-在main()c = create_control_point()行上失败:

from brisa.core.reactors import install_default_reactor

reactor = install_default_reactor()
print reactor

from brisa.core.threaded_call import run_async_function
from brisa.upnp.control_point.control_point import ControlPoint
from datetime import datetime

service = ('u', 'urn:schemas-upnp-org:service:SwitchPower:1')
binary_light_type = 'urn:schemas-upnp-org:device:BinaryLight:1'

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

def get_switch_service(device):
    return device.services[service[1]]

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

def main():
    """ Main loop iteration receiving input commands.
    """
    print "hell"
    c = create_control_point()
    print "helllo"
    c.start()
    run_async_function(_handle_cmds, (c, ))
    reactor.add_after_stop_func(c.stop)
    reactor.main()

def _exit(c):
    """ Stops the _handle_cmds loop
    """
    global running_handle_cmds
    running_handle_cmds = False

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 _get_status(c):
    """ Gets the binary light status and print if it's on or off.
    """
    try:
        service = get_switch_service(c.current_server)
        status_response = service.GetStatus()
        if status_response['ResultStatus'] == '1':
            print 'Binary light status is on'
        else:
            print 'Binary light status is off'
    except Exception, e:
        if not hasattr(c, 'current_server') or not c.current_server:
            print 'BinaryLight device not set.Please use set_light <n>'
        else:
            print 'Error in get_status():', e

def _get_target(c):
    """ Gets the binary light target and print if it's on or off.
    """
    try:
        service = get_switch_service(c.current_server)
        status_response = service.GetTarget()
        if status_response['RetTargetValue'] == '1':
            print 'Binary light target is on'
        else:
            print 'Binary light target is off'
    except Exception, e:
        if not hasattr(c, 'current_server') or not c.current_server:
            print 'BinaryLight device not set.Please use set_light <n>'
        else:
            print 'Error in get_target():', e

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

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

I am getting the output: 我得到的输出:

ankit@ubuntu:~/Desktop$ python controlpt.py

<brisa.core.reactors.glib2.GLib2Reactor object at 0x88c3e0c>
hello
hell

Why isn't it getting to the c = create_control_point() line? 为什么不到达c = create_control_point()行? Also, the controlpoint constructor is quitting the whole program. 同样, controlpoint构造函数正在退出整个程序。
When I tried 当我尝试

$ python
>>> import controlpt
>>> controlpt.main()

I got the output: 我得到了输出:

>>> controlpt.main()
hell
ankit@ubuntu:~/Desktop$ 

Help? 救命?

Since it's dying on the ControlPoint initialization, have you tried the following in the interpreter? 由于它依赖于ControlPoint初始化,因此您是否在解释器中尝试了以下操作?

from brisa.upnp.control_point.control_point import ControlPoint
c = ControlPoint()

If that works, then there's something else at play. 如果这行得通,那么还有其他作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM