Title: Calling long running C# from Python: Do I really need to call PythonEngine.BeginAllowThreads() ? · pythonnet/pythonnet · Discussion #2424 · GitHub
Open Graph Title: Calling long running C# from Python: Do I really need to call PythonEngine.BeginAllowThreads() ? · pythonnet/pythonnet · Discussion #2424
X Title: Calling long running C# from Python: Do I really need to call PythonEngine.BeginAllowThreads() ? · pythonnet/pythonnet · Discussion #2424
Description: Calling long running C# from Python: Do I really need to call PythonEngine.BeginAllowThreads() ?
Open Graph Description: The following page https://github.com/pythonnet/pythonnet/wiki/Threading Says: However I wrote a simple test and found that calling PythonEngine.BeginAllowThreads() is not required i.e. the Python ...
X Description: The following page https://github.com/pythonnet/pythonnet/wiki/Threading Says: However I wrote a simple test and found that calling PythonEngine.BeginAllowThreads() is not required i.e. the Python ...
Opengraph URL: https://github.com/pythonnet/pythonnet/discussions/2424
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"QAPage","mainEntity":{"@type":"Question","name":"Calling long running C# from Python: Do I really need to call PythonEngine.BeginAllowThreads() ?","text":"The following page https://github.com/pythonnet/pythonnet/wiki/Threading
\nSays:
\n\nHowever I wrote a simple test and found that calling PythonEngine.BeginAllowThreads() is not required i.e. the Python script is not held up by the call to .NET.
\nPython 3.12 code:
\nfrom pathlib import Path\nimport threading\nimport time\nimport pythonnet\npythonnet.set_runtime(\"coreclr\")\nimport clr\n\n\nmy_path = Path(__file__).parent\nassembly_location = my_path / \"SimpleDotNetApiSolution\\\\bin\\\\Debug\\\\net7.0\\\\SimpleDotNetApi\"\nclr.AddReference(str(assembly_location))\nfrom SimpleDotNetApi import SimpleDotNetApiClass\n\n\ndef do_python_work():\n for i in range(10):\n print(f\"do_python_work {i}\")\n time.sleep(0.3)\n\n\nthreading.Thread(target=do_python_work).start()\nsimple_dotnet_api_obj = SimpleDotNetApiClass()\nresult = simple_dotnet_api_obj.DotNetDoWork()\nprint(\".NET returned:\", result)
\n.NET 7 code:
\nnamespace SimpleDotNetApi\n{\n public class SimpleDotNetApiClass\n {\n public bool DotNetDoWork()\n {\n for (int i = 0; i < 10; i++)\n {\n Console.WriteLine($\"DotNetDoWork {i}\");\n Thread.Sleep( 1000 );\n }\n return true;\n }\n }\n}
\nOutput:
\ndo_python_work 0\nDotNetDoWork 0\ndo_python_work 1\ndo_python_work 2\ndo_python_work 3\nDotNetDoWork 1\ndo_python_work 4\ndo_python_work 5\ndo_python_work 6\nDotNetDoWork 2\ndo_python_work 7\ndo_python_work 8\ndo_python_work 9\nDotNetDoWork 3\nDotNetDoWork 4\nDotNetDoWork 5\nDotNetDoWork 6\nDotNetDoWork 7\nDotNetDoWork 8\nDotNetDoWork 9\n.NET returned: True\n
\nAm I misunderstanding something?
\nCode attached: PythonCallingDotNetThreadingTest.zip
","upvoteCount":1,"answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"In #2209 (reply in thread) @lostmsu answered this question:
\n\nThis might be confusing. I believe by default now GIL is not held when .NET code is called from Python. So actually in order to work with any Python objects you have to call and hold Py.GIL().
\nThe wiki is outdated.
\n
","upvoteCount":1,"url":"https://github.com/pythonnet/pythonnet/discussions/2424#discussioncomment-11252913"}}}
| route-pattern | /_view_fragments/Voltron::DiscussionsFragmentsController/show/:user_id/:repository/:discussion_number/discussion_layout(.:format) |
| route-controller | voltron_discussions_fragments |
| route-action | discussion_layout |
| fetch-nonce | v2:75166600-6b5b-f382-3201-bd0bc81b85dc |
| current-catalog-service-hash | 9f0abe34da433c9b6db74bffa2466494a717b579a96b30a5d252e5090baea7be |
| request-id | 97C4:77C42:1C92C33:25B682B:6971A5A6 |
| html-safe-nonce | fc44e5416d86ce12eb01b484a23c41b02a6cf41c1c07c7fc4314e712290b05bc |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5N0M0Ojc3QzQyOjFDOTJDMzM6MjVCNjgyQjo2OTcxQTVBNiIsInZpc2l0b3JfaWQiOiIyNDE4MjE5OTI0MDQ0NDkwMTUwIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 3ca549c94b1c11562e398818ab4eda18d6cc2d93fbf532ebcca13753045ef508 |
| hovercard-subject-tag | discussion:6994198 |
| github-keyboard-shortcuts | repository,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/Voltron::DiscussionsFragmentsController/show/pythonnet/pythonnet/2424/discussion_layout |
| twitter:image | https://opengraph.githubassets.com/fda5c1020fdf67be9b0070e0c5e0730f9e7744013c32d6e0fc77fcf298f8ba74/pythonnet/pythonnet/discussions/2424 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/fda5c1020fdf67be9b0070e0c5e0730f9e7744013c32d6e0fc77fcf298f8ba74/pythonnet/pythonnet/discussions/2424 |
| og:image:alt | The following page https://github.com/pythonnet/pythonnet/wiki/Threading Says: However I wrote a simple test and found that calling PythonEngine.BeginAllowThreads() is not required i.e. the Python ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | fdfdce9cd4f6ab85dca2b0d11264270829297c962dd5a79df449062d7822258f |
| turbo-cache-control | no-preview |
| go-import | github.com/pythonnet/pythonnet git https://github.com/pythonnet/pythonnet.git |
| octolytics-dimension-user_id | 6050430 |
| octolytics-dimension-user_login | pythonnet |
| octolytics-dimension-repository_id | 14748123 |
| octolytics-dimension-repository_nwo | pythonnet/pythonnet |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 14748123 |
| octolytics-dimension-repository_network_root_nwo | pythonnet/pythonnet |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 51c736e60b302bd039c9d5164573d176ceb24bb2 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width