我想将数据库句柄从我的perl脚本传递给我的python脚本

问题描述 投票:0回答:1

我想将数据库句柄从我的perl脚本传递给我的python脚本。我想确保python脚本与perl在同一会话中运行。

到目前为止,我已经尝试过-

Perl包装器在传递数据库句柄时调用Python子例程。无论我将句柄作为dbh还是$ dbh传递都无效选项1

# ----------------------------------------------------------------- #
# # Connect to MIDB schema and get connection handle
# # ----------------------------------------------------------------- #
my $dbh = ConnDb::connect_to_midb(); 
#
# ----------------------------------------------------------------- #
# # Call Python subroutine b
# ----------------------------------------------------------------- #
py_eval(<<'END');
from test_phy import b
b(dbh)
END

$ perl c_pl
Traceback (most recent call last):
  File "<string>", line 2, in <module>
NameError: name 'dbh' is not defined
Error -- py_eval raised an exception at c_pl line 29.

Option2
# ----------------------------------------------------------------- #
# # Connect to MIDB schema and get connection handle
# # ----------------------------------------------------------------- #
my $dbh = MiDb::connect_to_midb();
#
# ----------------------------------------------------------------- #
# # Import python subroutine b
# ----------------------------------------------------------------- #
#
py_eval(<<'END');
from test_phy import b
END

# ----------------------------------------------------------------- #
# Call Python subroutine.
# ----------------------------------------------------------------- #
py_bind_func("main::Bar", "__main__", "b($dbh)");
print "The meaning of life is: ", Bar(), "\n";

$ perl a_pl
'b(DBI::db=HASH(0x23e6870))' is not a callable object at (eval 20) line 3.
python perl
1个回答
0
投票

理想情况下,您不应将数据库句柄传递给另一个进程。执行该操作,然后关闭连接并在第二步中需要时打开。更不用说从Perl传递到python了,这没有任何意义。

© www.soinside.com 2019 - 2024. All rights reserved.