如何在单独的线程中执行 IronPython 脚本然后挂起它?

发布于 2024-09-28 22:30:51 字数 1657 浏览 6 评论 0原文

假设我有这样的代码用于测试。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

触ぅ动初心 2024-10-05 22:30:51

Thread 类具有 Suspend()Resume() 方法。这些方法挂起和恢复线程。尽管有关于此方法的警告,但这应该不是问题,因为您正在已知位置暂停。

另一种方法是使用事件。您可以为 ScriptScriptScope 类提供 AutoResetEvent。然后,在 Sleep() 中,您对事件调用 WaitOne()。然后,从外部,当您希望线程恢复时,您可以调用 Set()

The Thread class has a Suspend() and Resume() 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 or ScriptScope class a AutoResetEvent. Then, in the Sleep(), you call WaitOne() on the event. Then, from the outside, when you want the thread to resume, you call Set().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文