From C# we have to call python function, in this case the nltk lib lemmatize function (which still has no good C# implementation). We call it like this:
private string Lemmatize(string word)
{
using (Py.GIL())
{
using (var scope = Py.CreateScope())
{
dynamic nltk = Py.Import("nltk");
var lemmatizer = nltk.stem.WordNetLemmatizer();
var lemma = lemmatizer.lemmatize(word);
return lemma;
}
}
}
It works perfectly, in the test application, from a .net console app. When we apply this into the real app, which has async functions we experienced that the function usually hang at the point using (Py.GIL())
. The whole app runs single threaded, because async functions everywhere, but each of them is called with await
. Therefore a calling point looks like this:
private async Task<IList<string>> LemmatizeAll(IEnumerabe<string> words)
{
var result = new List<string>();
foreach(var word in words)
{
var item = Lemmatize(word);
result.Add(item);
}
return result;
}
//
var lemmas = await LemmatizeAll(new [] {"running", "man"});
I understand that the Py.GIL()
is mostly a semaphore, not enabling running multiple threads at the same time, but this not what happens now. It runs from only one thread, however from different threads because of the async-await. To be hung is not a nice thing from it.
How to use the python scripts from an environment like this? Any advice?
5