【问题标题】:Program not executing full, stops in the middle程序未完全执行,中途停止
【发布时间】:2011-02-15 01:56:56
【问题描述】:
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()

我得到了输出:

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

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

它不会进入 c = create_control_point 行。这是为什么?请指导我消除此错误。

【问题讨论】:

  • 我建议您打印更多有用的 cmets,例如“Finished create_control_point”和“Inside create_control_point”,而不是使用不同数量的 ls 对“Hello”进行不同的变体。

标签: python dictionary service


【解决方案1】:

我不熟悉这个“brisa”库。我认为,出于某种原因,ControlPoint 构造函数正在使用 sys.exit() 退出整个程序(或者正在调用一些等效的 C 代码)。

在命令行上运行程序,如下所示:

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

然后你应该能够看到它是否真的退出了整个过程。如果它让你回到 >>> 提示符,那么我不知道发生了什么。但如果它完全退出 Python,则意味着 ControlPoint 构造函数正在退出。

【讨论】:

  • 是的...你说得对,控制点构造函数正在退出。我应该如何使它正确?你可以在这里看到 Brisa 图书馆:brisa.garage.maemo.org/apidoc/index.html..plz 给我建议。
  • 我看了一眼那些文件。除了 ControlPoint 构造函数使用大量代码调用 lot 的其他构造函数之外,没有什么明显的想法浮现在脑海中。其中任何一个都可能导致它。我会寻找对 C 代码的调用;这可能更有可能退出(也许某些 C 代码在错误条件下退出,而不是引发适当的 Python 异常)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-24
  • 1970-01-01
  • 1970-01-01
  • 2020-10-28
  • 2021-03-10
相关资源
最近更新 更多