// Constructed via reflection when deserialized from the registry. public RemotePythonEvaluator() { _factory = CreateFactory(); }
public override void Restart() { WriteLine("Remote process has exited, restarting..."); _factory = CreateFactory(); Start(); _factory.CommandDispatcher = _engine.GetService<PythonService>(_engine).GetLocalCommandDispatcher(); var changed = AvailableScopesChanged; if (changed != null) { changed(this, EventArgs.Empty); } }
public void Scenario_RemoteScriptFactory() { using (var factory = RemotePythonEvaluator.CreateFactory()) { var runtime = (ScriptRuntime)factory.CreateRuntime(Python.CreateRuntimeSetup(new Dictionary<string, object>())); StringWriter writer = new StringWriter(); StringWriter errWriter = new StringWriter(); runtime.IO.SetOutput(Stream.Null, writer); factory.SetConsoleOut(writer); factory.SetConsoleError(errWriter); // verify print goes to the correct output var engine = runtime.GetEngine("Python"); engine.Execute("print 'hello'"); var builder = writer.GetStringBuilder(); AreEqual(builder.ToString(), "hello\r\n"); builder.Clear(); // verify Console.WriteLine is redirected engine.Execute("import System\nSystem.Console.WriteLine('hello')\n"); AreEqual(builder.ToString(), "hello\r\n"); builder.Clear(); // verify Console.Error.WriteLine is redirected to stderr var errBuilder = errWriter.GetStringBuilder(); engine.Execute("import System\nSystem.Console.Error.WriteLine('hello')\n"); AreEqual(errBuilder.ToString(), "hello\r\n"); errBuilder.Clear(); // raise an exception, should be propagated back try { engine.Execute("import System\nraise System.ArgumentException()\n"); AreEqual(true, false); } catch (ArgumentException) { } /* // verify that all code runs on the same thread var scope = engine.CreateScope(); engine.Execute("import System"); List<object> res = new List<object>(); for (int i = 0; i < 100; i++) { ThreadPool.QueueUserWorkItem( (x) => { object value = engine.Execute("System.Threading.Thread.CurrentThread.ManagedThreadId", scope); lock (res) { res.Add(value); } }); } while (res.Count != 100) { Thread.Sleep(100); } for (int i = 1; i < res.Count; i++) { if (!res[i - 1].Equals(res[i])) { throw new Exception("running on multiple threads"); } }*/ // create a long running work item, execute it, and then make sure we can continue to execute work items. ThreadPool.QueueUserWorkItem(x => { engine.Execute("while True: pass"); }); Thread.Sleep(1000); factory.Abort(); AreEqual(engine.Execute("42"), 42); } // check starting on an MTA thread using (var factory = new RemoteScriptFactory(ApartmentState.MTA)) { var runtime = (ScriptRuntime)factory.CreateRuntime(Python.CreateRuntimeSetup(new Dictionary<string, object>())); var engine = runtime.GetEngine("Python"); AreEqual(engine.Execute("import System\nSystem.Threading.Thread.CurrentThread.ApartmentState == System.Threading.ApartmentState.MTA"), true); } // check starting on an STA thread using (var factory = new RemoteScriptFactory(ApartmentState.STA)) { var runtime = (ScriptRuntime)factory.CreateRuntime(Python.CreateRuntimeSetup(new Dictionary<string, object>())); var engine = runtime.GetEngine("Python"); AreEqual(engine.Execute("import System\nSystem.Threading.Thread.CurrentThread.ApartmentState == System.Threading.ApartmentState.STA"), true); } }
// Constructed via reflection when deserialized from the registry. public RemoteRubyEvaluator() { _factory = CreateFactory(); }
public override void Restart() { WriteLine("Remote process has exited, restarting..."); _factory = CreateFactory(); Start(); var changed = AvailableScopesChanged; if (changed != null) { changed(this, EventArgs.Empty); } }
public override void Reset() { // TODO: strange text buffer behavior (race condition?) // WriteLine("Remote process has been reset..."); _factory.Shutdown(); _factory = CreateFactory(); Start(); var changed = AvailableScopesChanged; if (changed != null) { changed(this, EventArgs.Empty); } }