Search code examples
pythonraspberry-piweb.py

python and global variables


How do I setup the global variable properly?

# default objects is being set as None (null), after importing the piCamera library
camera = None

after that I'm declaring the Webcam class with the GET definition:

class Webcam:
    def GET(self):
        web.header('Content-type','multipart/x-mixed-replace; boundary=--jpgboundary')
        stream=io.BytesIO()
        start=time.time()

        for foo in camera.capture_continuous(stream,'jpeg'):
            web.header('Content-type','--jpgboundary')
            web.header('Content-type','image/jpeg')
            web.header('Content-length',len(stream.getvalue()))
            yield (stream.getvalue())
            stream.seek(0)
            stream.truncate()
            time.sleep(.5)


if __name__ == "__main__":
    camera = picamera.PiCamera()
    global camera
    camera.resolution = (640, 480)
    app = web.application(urls, globals())
    app.internalerror = web.debugerror
    app.run()

Should I put the if __name__ == "__main__": before I'm defining the Webcam class?

Here's my traceback of the calls:

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/wsgiserver/__init__.py", line 1245, in communicate
    req.respond()
  File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/wsgiserver/__init__.py", line 775, in respond
    self.server.gateway(self).respond()
  File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/wsgiserver/__init__.py", line 2018, in respond
    response = self.req.server.wsgi_app(self.env, self.start_response)
  File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/httpserver.py", line 306, in __call__
    return self.app(environ, xstart_response)
  File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/httpserver.py", line 274, in __call__
    return self.app(environ, start_response)
  File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/application.py", line 281, in wsgi
    result = peep(result)
  File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/application.py", line 261, in peep
    firstchunk = iterator.next()
  File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/application.py", line 605, in wrap
    yield next()
  File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/application.py", line 597, in next
    return result.next()
  File "/home/pi/piApp/index.py", line 77, in GET
    for foo in camera.capture_continuous(stream,'jpeg'):
AttributeError: 'NoneType' object has no attribute 'capture_continuous'

Solution

  • It looks like you are running this as a library file and therefore the __main__ section does not execute. Put that code at global scope.

    I would guess something like this is what you want:

    def main():
        global camera
        camera = picamera.PiCamera()
        camera.resolution = (640, 480)
        app = web.application(urls, globals())
        app.internalerror = web.debugerror
        app.run()
    
    main()