2014-07-13 4 views
0

어떻게 전역 변수를 올바르게 설정합니까?파이썬 및 전역 변수

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

그 후 나는 GET 정의와 웹캠 클래스를 선언하고 있습니다 : 나는 웹캠 클래스를 정의하고 있습니다 전에

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

내가 if __name__ == "__main__":를 넣어해야 하는가? 당신이 라이브러리 파일로이 작업을 실행하므로 __main__ 섹션은 실행되지 않습니다처럼

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' 
+0

글로벌 키워드는하자 당신이에서 전역을 모듈에 할당 작은 범위. if 문은 범위를 만들지 않기 때문에 여기서는 아무 일도하지 않습니다. – geoffspear

+0

@Wooble : 다른 대안을 제안 해 주시겠습니까? 것은 다른 클래스에서 카메라에 액세스하고 싶습니다. –

답변

0

것 같습니다 :

다음은 통화 내 역 추적입니다. 코드를 전역 범위에 두십시오.

나는 이런 식으로 뭔가를 추측 할 당신이 원하는 무엇인가 :

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

main() 
0

당신이 V4L 드라이버가 있는지 확인이 시도 :

sudo modprobe bcm2835-v4l2