Go/Python异步桥

2024-09-27 21:24:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我编写了一个客户端来处理较低级别的TLS连接参数,如ClientHellos等

我是在围棋里做的,因为那里容易多了。我的主程序(webscraper)是用Python编写的。我通过ctypes通过DLL将Go源代码连接到Python文件。到目前为止,我的webscraper结构是异步的,一次可以处理多个连接

不幸的是,我的Go客户端不是异步的。是否有一种方法可以在Python中更改它,使它异步等待ctypes指针的响应,直到它出现?现在它正在等待响应,但同时会阻止所有其他代码的执行

编辑: 下面的代码示例

async def request(self, method, url, headers, body=None, rawBody=None, pseudoHeaderOrder=["method", "authority", "scheme", "path"]):
        global httpLib
        global initFunc
        global requestFunc
        global changeProxyFunc
        global freePointerFunc
        config = {
            "id": self.cid,
            "method": method.upper(),
            "timeout": 20000,
            "url": url,
            "pseudoHeaderOrder": pseudoHeaderOrder,
            "headers": headers
        }
        #Critical
        if body:
            config["body"] = body
        if rawBody:
            rawBody = [b for b in bytes(rawBody, "utf-8")]
            config["rawBody"] = rawBody
        config = json.dumps(config)
        #print(config)
        #THIS PART CASTS THE REQUEST
        ptr = requestFunc(config.encode('utf-8'))
        string = ctypes.cast(ptr, ctypes.c_char_p).value.decode("utf-8")
        #THIS PART CLEARS THE POINTER
        freePointerFunc(ptr)
        #...

Tags: 代码configurlgo客户端bodyctypesglobal
1条回答
网友
1楼 · 发布于 2024-09-27 21:24:44

可以使用executor将阻塞调用移动到单独的线程/进程

这样应该行得通

async def request(self, method, url, headers, body=None, rawBody=None, pseudoHeaderOrder=["method", "authority", "scheme", "path"]):
        global httpLib
        global initFunc
        global requestFunc
        global changeProxyFunc
        global freePointerFunc
        config = {
            "id": self.cid,
            "method": method.upper(),
            "timeout": 20000,
            "url": url,
            "pseudoHeaderOrder": pseudoHeaderOrder,
            "headers": headers
        }
        #Critical
        if body:
            config["body"] = body
        if rawBody:
            rawBody = [b for b in bytes(rawBody, "utf-8")]
            config["rawBody"] = rawBody
        config = json.dumps(config)
        
        # Move blocking code to separate function
        def blocking_io():
          ptr = requestFunc(config.encode('utf-8'))
          string = ctypes.cast(ptr, ctypes.c_char_p).value.decode("utf-8")
          freePointerFunc(ptr)
          return string

        # Aschronously wait on the result
        loop = asyncio.get_running_loop()
        string = await loop.run_in_executor(None, blocking_io)
        
        #...

相关问题 更多 >

    热门问题