python调用lua_Python 通过 lupa 调用 lua,传入 table 类型,报错 cannot mix objects

test.lua

function test1(params)

print(type(params),params)

return 'test1:'..params

end

-- 入口函数,实现反射函数调用

function functionCall(func_name,params)

local is_true,result

local sandBox = function(func_name,params)

local result

result = _G[func_name](params)

return result

end

is_true,result= pcall(sandBox,func_name,params)

return result

end

# -*-coding:utf-8-*-

import traceback

from lupa import LuaRuntime

import threading

class Executor(threading.Thread):

"""

执行 lua 的线程类

"""

lock = threading.RLock()

luaScriptContent = None

luaRuntime = None

def __init__(self,api,params):

threading.Thread.__init__(self)

self.params = params

self.api = api

def run(self):

try:

# 执行具体的函数,返回结果打印在屏幕上

luaRuntime = self.getLuaRuntime()

rel = luaRuntime(self.api, self.params)

print rel

except Exception,e:

print e.message

traceback.extract_stack()

def getLuaRuntime(self):

"""

从文件中加载要执行的 lua 脚本,初始化 lua 执行环境

"""

# 上锁,保证多个线程加载不会冲突

if Executor.lock.acquire():

if Executor.luaRuntime is None:

fileHandler = open('./test.lua')

content = ''

try:

content = fileHandler.read()

except Exception, e:

print e.message

traceback.extract_stack()

# 创建 lua 执行环境

Executor.luaScriptContent = content

luaRuntime = LuaRuntime()

luaRuntime.execute(content)

# 从 lua 执行环境中取出全局函数 functionCall 作为入口函数调用,实现 lua 的反射调用

g = luaRuntime.globals()

function_call = g.functionCall

Executor.luaRuntime = function_call

Executor.lock.release()

return Executor.luaRuntime

if __name__ == "__main__":

lua=LuaRuntime()

table_data=lua.eval('{ a=1, b=2 }')

print(type(table_data),table_data) #(, )

executor1 = Executor('test1',"hello world") #正常,python 的字符串格式,在 lua 中也是字符串,输出:string hello world

executor2 = Executor('test1',{"a":1,"b":2}) #正常,python 的字典格式转化成 lua 的 userdata 类型,输出:userdata {'a': 1, 'b': 2}

executor3 = Executor('test1',table_data) #报错,将 lua 的 table 的地址传入,输出:cannot mix objects from different Lua runtimes

executor1.start()

executor2.start()

executor3.start()

executor1.join()

executor2.join()

executor3.join()

现在是有两个问题,解决任意一个都可以。。

一个是,lua 怎么可以把,userdata 类型转化成 table 类型

另一个是,传入 table 类型的变量,为什么会报错。。

我已经把 5.1 和 5.3 的版本都试过了,都报同一个错。。。

第一次发帖就提问。。不知道也可以留个言。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值