Skip to content

Instantly share code, notes, and snippets.

@hanslovsky
Last active May 10, 2021 02:48
Show Gist options
  • Save hanslovsky/bb6fe824682502863e08cee205f89593 to your computer and use it in GitHub Desktop.
Save hanslovsky/bb6fe824682502863e08cee205f89593 to your computer and use it in GitHub Desktop.
JEP threadsafe interpreter
import java.lang.Class
import java.lang.Thread
import java.util.concurrent.Callable
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
import jep.Interpreter
import jep.SharedInterpreter
class ThreadsafeInterpreter2(
private val initPython: () -> Interpreter = { SharedInterpreter() },
private val threadName: String = "Python"
): Interpreter {
inner class ThreadFactory : java.util.concurrent.ThreadFactory {
override fun newThread(r: Runnable): Thread {
val t = Thread {
python = SharedInterpreter()
try {
r.run()
} finally {
python.close()
}
}
t.setDaemon(true)
t.name = threadName
return t
}
}
private lateinit var python: Interpreter
private val executor = Executors.newSingleThreadExecutor(ThreadFactory())
override fun close() {
val f = exec { python.close() }
executor.shutdown()
f.get()
}
override fun eval(str: String) = exec { python.eval(str) }.get()
override fun exec(str: String) = exec { python.exec(str) }.get()
override fun runScript(script: String) = exec { python.runScript(script) }.get()
override fun getValue(str: String) = exec { python.getValue(str) }.get()
override fun <T> getValue(str: String, clazz: Class<T>) = exec { python.getValue(str, clazz) }.get()
override operator fun set(name: String, v: Any?) = exec { python.set(name, v) }.get()
override operator fun invoke(name: String, vararg args: Any) = exec { python.invoke(name, *args) }.get()
override operator fun invoke(name: String, kwargs: Map<String, Any>) = exec { python.invoke(name, kwargs) }.get()
override operator fun invoke(name: String, args: Array<Any>, kwargs: Map<String, Any>) = exec { python.invoke(name, kwargs) }.get()
operator fun get(str: String) = getValue(str)
private fun <T> exec(task: () -> T) = executor.submit(Callable { task() })
}
import java.lang.Class
import java.lang.Thread
import java.util.concurrent.Callable
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
import jep.Interpreter
import jep.SharedInterpreter
class ThreadsafeInterpreter3(
private val initPython: () -> Interpreter = { SharedInterpreter() },
threadName: String = "Python"
): Interpreter {
private lateinit var python: Interpreter
private val executor = Executors.newSingleThreadExecutor { r -> Thread(r.wrapped, threadName).also { it.setDaemon(true) } }
override fun close() {
val f = exec { python.close() }
executor.shutdown()
f.get()
}
override fun eval(str: String) = exec { python.eval(str) }.get()
override fun exec(str: String) = exec { python.exec(str) }.get()
override fun runScript(script: String) = exec { python.runScript(script) }.get()
override fun getValue(str: String) = exec { python.getValue(str) }.get()
override fun <T> getValue(str: String, clazz: Class<T>) = exec { python.getValue(str, clazz) }.get()
override operator fun set(name: String, v: Any?) = exec { python.set(name, v) }.get()
override operator fun invoke(name: String, vararg args: Any) = exec { python.invoke(name, *args) }.get()
override operator fun invoke(name: String, kwargs: Map<String, Any>) = exec { python.invoke(name, kwargs) }.get()
override operator fun invoke(name: String, args: Array<Any>, kwargs: Map<String, Any>) = exec { python.invoke(name, kwargs) }.get()
operator fun get(str: String) = getValue(str)
private fun <T> exec(task: () -> T) = executor.submit(Callable { task() })
private val Runnable.wrapped: Runnable get() = Runnable {
python = SharedInterpreter()
try {
this.run()
} finally {
python.close()
}
}
}
import java.lang.Class
import java.lang.Thread
import java.util.concurrent.Callable
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
import jep.Interpreter
import jep.SharedInterpreter
class ThreadsafeInterpreter(
private val initPython: () -> Interpreter = { SharedInterpreter() },
threadName: String = "Python"
): Interpreter {
private lateinit var _python: Interpreter
private val python: Interpreter
get() {
if (!this::_python.isInitialized) _python = initPython()
return _python
}
private val executor = Executors.newSingleThreadExecutor { Thread(it, threadName).also { it.setDaemon(true) } }
override fun close() {
val f = exec { python.close() }
executor.shutdown()
f.get()
}
override fun eval(str: String) = exec { python.eval(str) }.get()
override fun exec(str: String) = exec { python.exec(str) }.get()
override fun runScript(script: String) = exec { python.runScript(script) }.get()
override fun getValue(str: String) = exec { python.getValue(str) }.get()
override fun <T> getValue(str: String, clazz: Class<T>) = exec { python.getValue(str, clazz) }.get()
override operator fun set(name: String, v: Any?) = exec { python.set(name, v) }.get()
override operator fun invoke(name: String, vararg args: Any) = exec { python.invoke(name, *args) }.get()
override operator fun invoke(name: String, kwargs: Map<String, Any>) = exec { python.invoke(name, kwargs) }.get()
override operator fun invoke(name: String, args: Array<Any>, kwargs: Map<String, Any>) = exec { python.invoke(name, kwargs) }.get()
operator fun get(str: String) = getValue(str)
private fun <T> exec(task: () -> T) = executor.submit(Callable { task() })
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment