JPyRust is a high-performance bridge library enabling Java applications to use the Python AI ecosystem with Production-Grade Performance.
The system achieves 778x speedup (7s → 9ms) through:
- Daemon Mode: Python stays warm with models pre-loaded
- UUID Isolation: Thread-safe concurrent request handling
- Task Dispatching: Multiple AI tasks via single daemon
[Java App] -> (JNI) -> [Rust Bridge] -> (stdin/stdout) -> [Python Daemon]
|
File-based IPC
input_{uuid}.dat
output_{uuid}.dat
- Controllers:
AIImageController,AITextController - Bridge:
JPyRustBridge.javawithexecuteTask()native method - Config:
application.ymlfor paths and settings
- Tech:
jni-rs,lazy_static - Role: Spawn and manage Python daemon lifecycle
- IPC: stdin/stdout with
EXECUTEprotocol - Resilience: Auto-restart on daemon crash
- File:
ai_worker.py - Mode: Persistent loop (not one-shot)
- Tasks: YOLO (image), SENTIMENT (text)
- Models: Loaded once at startup
EXECUTE <task_type> <request_id> <metadata...>
| Task | Metadata | Input File | Output File |
|---|---|---|---|
| YOLO | width height channels |
Raw BGR bytes | JPEG bytes |
| SENTIMENT | NONE |
UTF-8 text | UTF-8 result |
READY # Initialization complete
DONE <result> # Success
ERROR <message> # Failure
- Binary data (images) is complex over stdin
- UUID-named files enable concurrent safety
- Easy debugging (files persist for inspection)
{work_dir}/input_{uuid}.dat # Request data
{work_dir}/output_{uuid}.dat # Response data
[4 bytes: length (big-endian)] [N bytes: data]
| Metric | Value | Notes |
|---|---|---|
| First Request | ~7s | Model loading (YOLO) |
| Text Analysis | ~9ms | After warmup |
| Image Detection | ~60-100ms | Includes resize + inference |
| Memory (Daemon) | ~500MB | YOLO model in GPU/CPU |
- UUID for Everything: Never use fixed filenames
- Cleanup After Use: Delete temp files post-processing
- Graceful Errors: Return
ERRORmessage, don't crash - Flush Always: Use
flush=Trueon Python prints - Timeout Protection: 60s limit on daemon startup
-
Python (
ai_worker.py):def handle_newtask(request_id, metadata): # Read input_{request_id}.dat # Process # Write output_{request_id}.dat return "DONE result" TASK_HANDLERS["NEWTASK"] = handle_newtask
-
Java (
JPyRustBridge.java):public byte[] processNewTask(String input) { return execute("NEWTASK", "NONE", input.getBytes()); }
-
Controller:
@PostMapping("/api/ai/newtask") public ResponseEntity<String> newTask(@RequestBody String input) { return ResponseEntity.ok(bridge.processNewTask(input)); }
Last updated: 2026-01-26 (v2.0 Universal Bridge)