millions-queen-95930
02/17/2024, 3:38 AMfrom metaflow import FlowSpec
from metaflow import step
from my_pkg.library.databases import initialize_database
from my_pkg.library.ssh import SshTunnel
from my_pkg.settings.databases import DatabaseConfiguration
class TunnelingPipelineExample(FlowSpec):
@step
def start(self):
self.next(self.query_tables)
@step
def query_tables(self):
db, cursor = initialize_database()
cursor.execute("USE some_database;")
cursor.execute("SELECT * FROM some_table;")
for result in cursor.fetchall():
print(f"RESULT: {result}")
self.next(self.end)
@step
def end(self):
print(f"Completed pipeline!")
if __name__ == "__main__":
with SshTunnel(DatabaseConfiguration()):
TunnelingPipelineExample()
## Working Example
from my_pkg.library.databases import initialize_database
from my_pkg.library.ssh import SshTunnel
from my_pkg.settings.databases import ApplicationDatabaseUsa
if __name__ == "__main__":
with SshTunnel(DatabaseConfiguration()):
db, cursor = initialize_database()
cursor.execute("USE some_database;")
cursor.execute("SELECT * FROM some_table;")
for result in cursor.fetchall():
print(f"RESULT: {result}")
The same code succeeds without Metaflow, so I assume Metaflow is somehow causing the context manager to get called twice given the error message says the tunnel is already in use.dry-beach-38304
02/20/2024, 2:17 AMwith statement would run multiple times. You could possibly do something like:
if sys.argv[2] == "run" # Or whatever the proper number
with SshTunnel(...)
TunnelingPipelineExample()
else:
TunnelingPipelineExample()
the trick will be introspecting the command line properly to make it work. It should only call “run” once (when you do it). then it calls things like “step” or “batch”.millions-queen-95930
02/28/2024, 6:22 PMdry-beach-38304
02/28/2024, 9:30 PMmyflow.py step … instead of myflow.py run so it should only call itself once with run.millions-queen-95930
03/01/2024, 3:21 PM