Packages
snakepit
0.3.3
0.13.0
0.12.0
0.11.1
0.11.0
0.10.1
0.10.0
0.9.1
0.9.0
0.8.9
0.8.8
0.8.7
0.8.6
0.8.5
0.8.4
0.8.3
0.8.2
0.8.1
0.8.0
0.7.7
0.7.6
0.7.5
0.7.4
0.7.3
0.7.2
0.7.1
0.7.0
0.6.11
0.6.10
0.6.9
0.6.8
0.6.7
0.6.6
0.6.5
0.6.4
0.6.3
0.6.2
0.6.1
0.6.0
0.5.1
0.5.0
0.4.3
0.4.2
0.4.1
0.4.0
0.3.3
0.3.2
0.3.1
0.3.0
0.2.1
0.2.0
0.1.2
0.1.1
0.1.0
High-performance pooler and session manager for external language integrations. Supports Python, Node.js, Ruby, and more with gRPC streaming, session management, and production-ready process cleanup.
Current section
Files
Jump to
Current section
Files
priv/python/generic_bridge_v2.py
#!/usr/bin/env python3
"""
Generic Python Bridge for Snakepit V2
A minimal, framework-agnostic bridge that demonstrates the protocol
without dependencies on any specific ML framework.
This version uses the proper snakepit_bridge package structure for
robust, production-ready bridge implementations.
To create a custom adapter:
1. Create a new class that inherits from BaseCommandHandler
2. Override _register_commands() to register your command handlers
3. Implement your command handler methods
4. Pass an instance of your handler to ProtocolHandler
Example:
from snakepit_bridge import BaseCommandHandler, ProtocolHandler
class MyCustomHandler(BaseCommandHandler):
def _register_commands(self):
self.register_command("my_command", self.handle_my_command)
def handle_my_command(self, args):
return {"result": "processed", "input": args}
handler = ProtocolHandler(MyCustomHandler())
handler.run()
"""
import sys
import os
# Add the bridge package to Python path if not already installed
if __name__ == "__main__":
bridge_dir = os.path.dirname(os.path.abspath(__file__))
if bridge_dir not in sys.path:
sys.path.insert(0, bridge_dir)
from snakepit_bridge import GenericCommandHandler, ProtocolHandler
from snakepit_bridge.core import setup_graceful_shutdown, setup_broken_pipe_suppression
def main():
"""Main entry point."""
# Suppress broken pipe errors globally
setup_broken_pipe_suppression()
if len(sys.argv) > 1 and sys.argv[1] == "--help":
print("Generic Snakepit Bridge V2")
print("Usage: python generic_bridge_v2.py [--mode pool-worker] [--protocol json|msgpack|auto] [--quiet]")
print("")
print("Options:")
print(" --mode pool-worker Run in pool worker mode (required for Snakepit)")
print(" --protocol PROTOCOL Wire protocol to use: json, msgpack, or auto (default: auto)")
print(" --quiet Suppress startup messages")
print("")
print("This bridge provides an extensible architecture for creating custom adapters.")
print("See the module docstring for examples on how to create your own adapter.")
print("")
print("Default supported commands:")
handler = GenericCommandHandler()
for cmd in sorted(handler.get_supported_commands()):
print(f" {cmd}")
return
# Parse protocol from command line arguments
protocol = "auto" # default
if "--protocol" in sys.argv:
idx = sys.argv.index("--protocol")
if idx + 1 < len(sys.argv):
protocol = sys.argv[idx + 1]
if protocol not in ["json", "msgpack", "auto"]:
print(f"Invalid protocol: {protocol}. Using auto.", file=sys.stderr)
protocol = "auto"
# Check for quiet mode
quiet = "--quiet" in sys.argv
# Create protocol handler with generic command handler
command_handler = GenericCommandHandler()
protocol_handler = ProtocolHandler(command_handler, protocol=protocol, quiet=quiet)
# Set up graceful shutdown handling
setup_graceful_shutdown(protocol_handler)
# Start protocol handler
try:
protocol_handler.run()
except (KeyboardInterrupt, BrokenPipeError, IOError):
# Clean shutdown, suppress errors
os._exit(0)
except Exception as e:
from snakepit_bridge.core import safe_print
safe_print(f"Bridge error: {e}")
sys.exit(1)
if __name__ == "__main__":
main()