Reputation: 897
I am calling python from C# using the following code:
if (!PythonEngine.IsInitialized)
{
PythonEngine.Initialize();
PythonEngine.BeginAllowThreads();
}
//var pythonScriptCommand = string.Format("{0}", Path.Combine(AppContext.BaseDirectory, "scripts" + Path.DirectorySeparatorChar + "keyringtest.py")); //Path.Combine(AppContext.BaseDirectory, "PythonSampleSystemDiagnostic.py");
IntPtr gs = PythonEngine.AcquireLock();
using (PyScope scope = Py.CreateScope())
{
//foreach (var array in myCommand.Settings)
//{
// Console.WriteLine(string.Join(" ", array)); ;
//}
string fileContent = File.ReadAllText(Path.Combine(@"../../Source/AMD.Agent.Standard.DataExchange/Commands/PythonScripts/", ScriptName));
var file = PythonEngine.Compile(fileContent);
scope.Execute(file);
//dynamic accessToken = scope.Get("get_access_token");
dynamic dataExtract = scope.Get("get_result_http");
//_logger.Info(myCommand.Settings.ToString() + "123123123123");
var dict = myCommand.Settings.ToDictionary(x => x.SettingName, x => x.SettingValue);
var dict123 = JsonConvert.SerializeObject(dict);
dataExtract(token,url1,"PerformanceHistoryPeriod");
//Console.WriteLine(_apxApiService + "123123123123123");
}
PythonEngine.ReleaseLock(gs);
PythonEngine.Shutdown();
Referenced this link: https://mail.python.org/pipermail/pythondotnet/2010-December/001058.html
I am not sure how this works in the sense that if I call this code multiple times in different threads, do all the script executions run in parallel or do they run sequentially, based on the lock.
Upvotes: 1
Views: 1784
Reputation: 91
I don't think you can execute multiple threads simultaneously with Python.NET due to the global interpreter lock. You can still execute python code in the separate thread, just not multiple threads at the same time (at least as it stands for now).
An alternative would be using IronPython, which provides a .NET implementation of Python 2 syntax - it can execute code in multiple threads just fine.
You can read a bit more information about pros and cons of using Python.NET versus IronPython here:
https://www.alternetsoft.com/blog/python-net-iron-python-scripting
Upvotes: 2