Golang Gphoto2响应缓慢

时间:2017-05-29 01:29:48

标签: rest go cgo

我写的Go程序有点奇怪,我认为可能与我正在调用的C库有关。程序运行的时间越长,检索图片的响应时间就越长。我不是什么甚至可以被认为是一个可通过的C程序员,我仍然是新的从Go加载C。我在gphoto2库周围编写了一个简单的REST界面,允许我远程触发相机并检索照片。理想情况下,我会用它来创建一个游戏中时光倒流,所以我需要它来运行很长时间。这是包装器:

  o.s.i.codec.kryo.CompositeKryoRegistrar  : configured Kryo registration [40, java.io.File] with serializer org.springframework.integration.codec.kryo.FileSerializer
  o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
  o.s.j.e.a.AnnotationMBeanExporter        : Bean with name 'rabbitConnectionFactory' has been autodetected for JMX exposure
  o.s.j.e.a.AnnotationMBeanExporter        : Bean with name 'integrationMbeanExporter' has been autodetected for JMX exposure
  o.s.j.e.a.AnnotationMBeanExporter        : Located managed bean 'integrationMbeanExporter': registering with JMX server as MBean [org.springframework.integration.monitor:name=integrationMbeanExporter,type=IntegrationMBeanExporter]
  o.s.j.e.a.AnnotationMBeanExporter        : Located managed bean 'rabbitConnectionFactory': registering with JMX server as MBean [org.springframework.amqp.rabbit.connection:name=rabbitConnectionFactory,type=CachingConnectionFactory]
  o.s.i.monitor.IntegrationMBeanExporter   : Registering beans for JMX exposure on startup
  o.s.i.monitor.IntegrationMBeanExporter   : Registering MessageChannel nullChannel
  o.s.i.monitor.IntegrationMBeanExporter   : Located managed bean 'org.springframework.integration:type=MessageChannel,name=nullChannel': registering with JMX server as MBean [org.springframework.integration:type=MessageChannel,name=nullChannel]
  o.s.i.monitor.IntegrationMBeanExporter   : Registering MessageChannel output
  o.s.i.monitor.IntegrationMBeanExporter   : Located managed bean 'org.springframework.integration:type=MessageChannel,name=output': registering with JMX server as MBean [org.springframework.integration:type=MessageChannel,name=output]
  o.s.i.monitor.IntegrationMBeanExporter   : Registering MessageChannel errorChannel
  o.s.i.monitor.IntegrationMBeanExporter   : Located managed bean 'org.springframework.integration:type=MessageChannel,name=errorChannel': registering with JMX server as MBean [org.springframework.integration:type=MessageChannel,name=errorChannel]
  o.s.i.monitor.IntegrationMBeanExporter   : Registering MessageHandler errorLogger
  o.s.i.monitor.IntegrationMBeanExporter   : Located managed bean 'org.springframework.integration:type=MessageHandler,name=errorLogger,bean=internal': registering with JMX server as MBean [org.springframework.integration:type=MessageHandler,name=errorLogger,bean=internal]
  o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase -2147482648
  r.t.b.demo.source.GatewayApplication     : No active profile set, falling back to default profiles: default
  s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@56cfe111: startup date [Mon May 29 15:07:40 JST 2017]; parent: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@4fe767f3
  o.s.c.support.GenericApplicationContext  : Refreshing org.springframework.context.support.GenericApplicationContext@67de7a99: startup date [Mon May 29 15:07:40 JST 2017]; root of context hierarchy
  r.t.b.demo.source.GatewayApplication     : Started GatewayApplication in 0.189 seconds (JVM running for 10.957)
  o.s.amqp.rabbit.core.RabbitAdmin         : Failed to declare exchange: Exchange [name=output, type=topic, durable=true, autoDelete=false, internal=false, arguments={}], continuing... org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused
  o.s.integration.channel.DirectChannel    : Channel 'gateway:8181.output' has 1 subscriber(s).
  o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase 0
  o.s.i.endpoint.EventDrivenConsumer       : Adding {logging-channel-adapter:_org.springframework.integration.errorLogger} as a subscriber to the 'errorChannel' channel
  o.s.i.channel.PublishSubscribeChannel    : Channel 'gateway:8181.errorChannel' has 1 subscriber(s).
  o.s.i.endpoint.EventDrivenConsumer       : started _org.springframework.integration.errorLogger
  o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase 2147482647
  o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase 2147483647
  s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8181 (http)

这是来自libgphoto2-dev的C函数,它正在调用:

onclick = "sendForm(event);"

}

最后,这是输出。当我第一次启动程序时,只需几秒钟就可以检索到图片,经过几个小时后,它只需要2分钟。

camera := new(gphoto2go.Camera)
err := camera.Init()
if err > 0 {
    log.Println(err)
}
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
    c.JSON(200, gin.H{
        "message": "pong",
    })
})

r.GET("/shot", func(c *gin.Context) {
    //camera.Interrupt()
    cameraFilePath, err := camera.TriggerCaptureToFile()
    if err == 0 {

        cameraFileReader := camera.FileReader(cameraFilePath.Folder, cameraFilePath.Name)
        defer cameraFileReader.Close()
        buf := new(bytes.Buffer)
        buf.ReadFrom(cameraFileReader)

        //camera.DeleteFile(cameraFilePath.Folder, cameraFilePath.Name)
        encodedImage := base64.StdEncoding.EncodeToString(buf.Bytes())
        c.JSON(200, gin.H{
            "image": encodedImage,
        })
        buf.Reset()
        //c.Data(200, "image/jpeg", buf.Bytes())

    } else {
        log.Println(gphoto2go.CameraResultToString(err))
        c.Error(errors.New(gphoto2go.CameraResultToString(err)))
        c.JSON(http.StatusInternalServerError, gphoto2go.CameraResultToString(err))
        return
    }

})
r.Run()

0 个答案:

没有答案