dongzhi8487 2018-02-12 19:12
浏览 96
已采纳

uWSGI +构建Go .so无法正常工作

Problem: .so(shared object) as library in python works well when python calls it and fails in uWSGI-running python(Django) application.

More info: I've build Go module with go build -buildmode=c-shared -o output.so input.go to call it in Python with

from ctypes import cdll

lib = cdll.LoadLibrary('path_to_library/output.so')

When django project is served via uWSGI the request handler that calling Go library freezes, causing future 504 in Nginx. After getting in "so called freeze", uWSGI is locked there and only restarting helps to enliven app. No logs AT ALL! It just freezes.

Everything works correctly when i run in python interpreter on the same machine.

My thoughts: i've tried to debug this and put a lot of log messages in library, but it won't give much info because everything is fine with library(because it works in interpreter). Library loads correctly, because some log messages that i've putted in library. I think it some sort of uWSGI limitation. I don't think putting uwsgi.ini file is somehow helpful.

Additional info:

What limitations can be in uWSGI in that type of shared object work and if there a way to overcome them?

  • 写回答

3条回答 默认 最新

  • dsfdsf21321 2018-02-21 20:41
    关注

    Attempts and thoughts

    I tried to avoid separation of shared library from my python code, since it requires support of at least one more process, and i would have to rewrite some of the library to create new api.

    As @Lu.nemec kindly noted that:

    Go has its own runtime, thread management and may not work well with uWSGI which handles threads/processes in a different way

    Since uWSGI is the problem i started seaching for a solution there. One of the hopes was installing GCCGO uWSGI plugin somehow solve that problem. But even it's hard to install on old OSes, because it lacks of pre-builded plugins and manual build haven't gone very well, it haven't helped, nothing changes, it still freezes.

    And then i thought that i wan't to disable coroutines and that type of stuff that differs from uWSGI and one of the changes that i am able to do is to set GOMAXPROCS

    GOMAXPROCS sets the maximum number of CPUs that can be executing simultaneously and returns the previous setting. If n < 1, it does not change the current setting. The number of logical CPUs on the local machine can be queried with NumCPU. This call will go away when the scheduler improves.

    And it worked like a charm!!!

    The solution

    import (
        ...
        "runtime"
    )
    ...
    //export yourFunc
    func yourFunc(yourArgs argType) {
        runtime.GOMAXPROCS(1)
        ...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突