0

I want to pass the db handle from my perl script to my python script. I want to ensure that python script runs in the same session as the perl one.

I'v tried this so far -

Perl wrapper to call Python subroutine while passing DB Handle. Doesn’t work whether I pass the handle as dbh or $dbh Option1

# ----------------------------------------------------------------- #
# # 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.
3
  • 2
    Where is py_eval coming from? Commented Jan 8, 2020 at 7:57
  • 2
    The concept doesn't make much sense, especially if we're talking about different processes. Values can be passed between processes, but you're talking about an object that doesn't represent a value. You'd also have to bring in the class, and you need a Perl interpreter for that. If it's same process, some kind of proxy could be used to allow the Python interpreter to call code in the Perl interpreter.... But why. Commented Jan 8, 2020 at 15:18
  • 1
    In addition, database connections should not be shared between processes in general. Commented Jan 8, 2020 at 21:23

1 Answer 1

1

Ideally, you should not pass DB handle to another process. Do the operation then close connection and open when you need it in the 2nd process. Let alone passing from Perl to python which doesn't make sense.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.