- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我目前正在尝试使用 Py4J 执行以下操作:
这是我在 Java 方面的一些东西:
package javabridge.test;
public interface PythonCallback {
Object notify(Object source);
}
package javabridge.test;
public class ScheduledRunnable implements Runnable {
private PythonCallback callback;
public ScheduledRunnable(PythonCallback callback) {
this.callback = callback;
}
@Override
public void run() {
System.out.println("[ScheduledRunnable] run -> notify");
this.callback.notify(this);
}
}
package javabridge.test;
import py4j.GatewayServer;
public class Test {
private PythonCallback callback;
public Test(PythonCallback callback) {
this.callback = callback;
}
public void runSynchronous() {
System.out.println("[runSynchronous] run -> notify");
this.callback.notify(this);
}
public void runAsynchronous() {
System.out.println("[runAsynchronous] run -> spawn thread");
ScheduledRunnable runnable = new ScheduledRunnable(callback);
Thread t = new Thread(runnable);
t.start();
}
public static void main(String[] args) {
GatewayServer server = new GatewayServer();
server.start(true);
}
}
在 Python 方面,我有以下脚本:
from py4j.java_gateway import JavaGateway, java_import, get_field, CallbackServerParameters
from py4j.clientserver import ClientServer, JavaParameters, PythonParameters
gateway = JavaGateway(callback_server_parameters=CallbackServerParameters())
#gateway = ClientServer(java_parameters=JavaParameters(), python_parameters=PythonParameters())
java_import(gateway.jvm, 'javabridge.test.*')
class PythonCallbackImpl(object):
def __init__(self, execfunc):
self.execfunc = execfunc
def notify(self, obj):
print('[PythonCallbackImpl] notified from Java')
self.execfunc()
return 'dummy return value'
class Java:
implements = ["javabridge.test.PythonCallback"]
def simple_fun():
print('[simple_fun] called')
gateway.jvm.System.out.println("[simple_fun] Hello from python!")
# Test 1: Without threading
input('Ready to begin test 1')
python_callback = PythonCallbackImpl(simple_fun)
nothread_executor = gateway.jvm.Test(python_callback)
nothread_executor.runSynchronous()
# Test 2: With threading
input('Ready to begin test 2')
python_callback = PythonCallbackImpl(simple_fun)
nothread_executor = gateway.jvm.Test(python_callback)
nothread_executor.runAsynchronous()
gateway.shutdown()
以下是尝试执行此脚本时发生的情况。首先,使用 gateway = ClientServer(java_parameters=JavaParameters(), python_parameters=PythonParameters())
,两个测试都失败了:
Test 1:
py4j.protocol.Py4JJavaError: An error occurred while calling o0.runSynchronous.
: py4j.Py4JException: Command Part is Empty or is the End of Command Part
at py4j.Protocol.getObject(Protocol.java:277)
at py4j.Protocol.getReturnValue(Protocol.java:458)
Test 2:
Exception in thread "Thread-4" py4j.Py4JException: Error while obtaining a new communication channel
at py4j.CallbackClient.getConnectionLock(CallbackClient.java:218)
at py4j.CallbackClient.sendCommand(CallbackClient.java:337)
at py4j.CallbackClient.sendCommand(CallbackClient.java:316)
但是,如果我注释掉 self.execfunc()
行,测试 1 会正常工作而不会引发错误。然而,测试 2 仍然失败:
Exception in thread "Thread-5" py4j.Py4JException: Error while sending a command.
at py4j.CallbackClient.sendCommand(CallbackClient.java:357)
at py4j.CallbackClient.sendCommand(CallbackClient.java:316)
现在切换到 gateway = JavaGateway(callback_server_parameters=CallbackServerParameters())
。当我将 self.execfunc()
注释掉时,测试 2 仍然在这里失败:
Exception in thread "Thread-5" py4j.Py4JException: Error while sending a command.
at py4j.CallbackClient.sendCommand(CallbackClient.java:357)
at py4j.CallbackClient.sendCommand(CallbackClient.java:316)
但至少测试 1 确实在启用 self.execfunc()
的情况下工作。
我的问题是:如何在 self.execfunc()
调用中使用线程方法? Py4J 可以做到这一点吗?
编辑:让事情变得更加棘手的是,self.execfunc()
调用的 Java 命令应该在调用 .notify()
的同一个 Java 线程中运行。
最佳答案
已解决。结果很简单:
然后 Java 将巧妙地遵循预期的线程模型,即接收 Python 回调调用的 Java 命令在执行回调的同一 Java 线程中执行。
通过一个简单的 Python 函数,可以添加一个 shutdown_when_done
方法,该方法等待所有回调都返回后再退出。
关于java - 来自 Java Runnable 的 Py4J 回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47607463/
好吧,我的问题听起来很困惑,但实际上很简单。我有一个 Runnable 启动另一个 Runnable 实例。所以基本上: 可运行 1 -> 可运行 2 runnable1 是在 runnable2 还
我正在尝试使用我的单例的 Handler.post() 方法从另一个 runnable 运行一个 runnable,但是直到原始 runnable 完成后才运行第二个 runnable。在下面的示例代
为什么下面的第一个例子不起作用? run(R::new); 方法 R.run 未被调用。 run(new R()); 方法 R.run 被调用。 这两个示例都是可编译的。 public class C
为什么下面的代码不起作用?基本上,这是一个更困难的程序的简化版本,在该程序中,我试图创建一个可运行的初始屏幕,其中包含一些选择,然后有链接到不同可运行项的按钮,但这并没有按照我的预期运行。 impor
我有一个带有主选项卡 Activity 的 Android 应用,以及各个选项卡中的多个 Activity 。在我的主要 Activity 的 onCreate() 中,我有一个创建列表的可运行文件,
我正在使用 Mockito 进行测试。我有一个回调接口(interface): interface Callback { void onMessageRetrieved(String mess
这个问题在这里已经有了答案: Does postDelayed cause the message to jump to the front of the queue? (1 个回答) 关闭 7 年
我想将 runnable 发布到 runnable 内的 View 对象,目前我被困在这里。 var runnable = Runnable { if(numLinesToDraw
假设我有一个 ExecutorService,我用它来运行 Job 对象,这是一个扩展 Runnable 的类。我希望我的 Job 对象并行运行,除了让 2 个 Job 对象具有相同的 job.get
下面是该类的源代码。 我想验证如何 shutdownNow()适用于未提交的任务。我在下面的代码中遇到的问题是 shutdownNow()返回 List而不是 List我已经提交了 List包含提交的
我正在尝试找出如何从多线程应用程序中获得最大性能。 我有一个这样创建的线程池: ExecutorService executor = Executors.newFixedThreadPool(8);
这有什么区别?请引用选项1和选项2。因为我遇到了麻烦,因为它们好像是一样的。它们运行正确 Thread ThreadPoolExecutor executor = (ThreadPoolExecuto
当任何命令在任何 ScheduledExecutorService 上以固定速率调度时,它返回 ScheduledFuture 也可以取消。但是“cancel”并不能保证命令在 cancel 返回后仍
因此,我希望在尝试保持轻资源负载的同时同时完成几件事。例如,同时播放声音和更新 GUI。让多个处理程序具有单个可运行对象或单个处理程序具有多个并行运行的可运行对象更好吗? 我知道下面的实现实际上不会同
这个问题已经有答案了: 已关闭11 年前。 Possible Duplicate: Java: “implements Runnable” vs. “extends Thread” 我只是想知道创建自
为什么 Java 的 scheduleWithFixedDelay 使用 Runnable 而不是包装 runnable 的 FutureTask? 这可以很容易地用两个不同的代码示例来展示: Sch
这个问题在这里已经有了答案: What's the difference between Activity.runOnUiThread(runnable action) and Handler.pos
我有如下三个类: 1.线程实例 public class ThreadInstance extends Thread { public ThreadInstance() { }
春天给了我下面的错误,却想不出为什么!。注意:在XML文件中没有对此的Bean定义。。我也提出了这一点,但没有为我的案件找到任何解决方案
一 Runnable 的职责 Runnable 接口非常简单,只定义了一个无参无返回值的 run 方法,源码如下。 @FunctionalInterface public interface Runn
我是一名优秀的程序员,十分优秀!