如何在单独的线程中执行 IronPython 脚本然后挂起它?
假设我有这样的代码用于测试。
public class SimpleScheduler
{
public Script Script { get; set; }
private Thread _worker;
public void Schedule()
{
this._worker = new Thread(this.Script.Execute);
this._worker.Start();
}
public void Sleep()
{
//?
}
}
SimpleScheduler
仅获取 Script
对象并尝试在单独的线程中执行它。
public class Script
{
public string ID { get; set; }
private ScriptSource _scriptSource;
private ScriptScope _scope;
private CompiledCode _code;
private string source = @"import clr
clr.AddReference('Trampoline')
from Trampoline import PythonCallBack
def Start():
PythonCallBack.Sleep()";
public Script()
{
_scriptSource = IronPythonHelper.IronPythonEngine.CreateScriptSourceFromString(this.source);
_scope = IronPythonHelper.IronPythonEngine.CreateScope();
_code = _scriptSource.Compile();
}
public void Execute()
{
_code.Execute(_scope);
dynamic start = _scope.GetVariable("Start");
start();
}
}
Script
类尝试回调 PythonCallBack
类的 Sleep 函数,并希望挂起一段时间。
public static class PythonCallBack
{
public static SimpleScheduler Scheduler;
static PythonCallBack()
{
Scheduler = new SimpleScheduler();
}
public static void Sleep()
{
Scheduler.Sleep();
}
}
PythonCallBack仅用于调用SimpleScheduler的sleep方法。
问题: 暂停执行脚本的线程的最佳方法是什么?那么如何恢复这个线程的执行呢?
Lets assume I have such code for testing.
public class SimpleScheduler
{
public Script Script { get; set; }
private Thread _worker;
public void Schedule()
{
this._worker = new Thread(this.Script.Execute);
this._worker.Start();
}
public void Sleep()
{
//?
}
}
SimpleScheduler
just takes Script
object and tries to execute it in separate thread.
public class Script
{
public string ID { get; set; }
private ScriptSource _scriptSource;
private ScriptScope _scope;
private CompiledCode _code;
private string source = @"import clr
clr.AddReference('Trampoline')
from Trampoline import PythonCallBack
def Start():
PythonCallBack.Sleep()";
public Script()
{
_scriptSource = IronPythonHelper.IronPythonEngine.CreateScriptSourceFromString(this.source);
_scope = IronPythonHelper.IronPythonEngine.CreateScope();
_code = _scriptSource.Compile();
}
public void Execute()
{
_code.Execute(_scope);
dynamic start = _scope.GetVariable("Start");
start();
}
}
Script
class tries to callback Sleep function of PythonCallBack
class and wants to be suspended for some time.
public static class PythonCallBack
{
public static SimpleScheduler Scheduler;
static PythonCallBack()
{
Scheduler = new SimpleScheduler();
}
public static void Sleep()
{
Scheduler.Sleep();
}
}
PythonCallBack just for calling sleep method of SimpleScheduler.
Question:
What is the best way to suspend thread, which executes Script? and then how to resume this thread execution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Thread
类具有Suspend()
和Resume()
方法。这些方法挂起和恢复线程。尽管有关于此方法的警告,但这应该不是问题,因为您正在已知位置暂停。另一种方法是使用事件。您可以为
Script
或ScriptScope
类提供AutoResetEvent
。然后,在Sleep()
中,您对事件调用WaitOne()
。然后,从外部,当您希望线程恢复时,您可以调用Set()
。The
Thread
class has aSuspend()
andResume()
method. These methods suspend and resume the thread. Even though there is a warning concerning this method, this should not be a problem because you are suspending at a known location.An alternative is to use events. You can give the
Script
orScriptScope
class aAutoResetEvent
. Then, in theSleep()
, you callWaitOne()
on the event. Then, from the outside, when you want the thread to resume, you callSet()
.