René's URL Explorer Experiment


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

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@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

\"image\"

\n

However 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.

\n

Python 3.12 code:

\n
from 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:

\n
namespace 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}
\n

Output:

\n
do_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
\n

Am I misunderstanding something?

\n

Code attached: PythonCallingDotNetThreadingTest.zip

","upvoteCount":1,"answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"

In #2209 (reply in thread) @lostmsu answered this question:

\n
\n

This 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().

\n

The 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-controllervoltron_discussions_fragments
route-actiondiscussion_layout
fetch-noncev2:75166600-6b5b-f382-3201-bd0bc81b85dc
current-catalog-service-hash9f0abe34da433c9b6db74bffa2466494a717b579a96b30a5d252e5090baea7be
request-id97C4:77C42:1C92C33:25B682B:6971A5A6
html-safe-noncefc44e5416d86ce12eb01b484a23c41b02a6cf41c1c07c7fc4314e712290b05bc
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5N0M0Ojc3QzQyOjFDOTJDMzM6MjVCNjgyQjo2OTcxQTVBNiIsInZpc2l0b3JfaWQiOiIyNDE4MjE5OTI0MDQ0NDkwMTUwIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmac3ca549c94b1c11562e398818ab4eda18d6cc2d93fbf532ebcca13753045ef508
hovercard-subject-tagdiscussion:6994198
github-keyboard-shortcutsrepository,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/discussions_fragments/discussion_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/Voltron::DiscussionsFragmentsController/show/pythonnet/pythonnet/2424/discussion_layout
twitter:imagehttps://opengraph.githubassets.com/fda5c1020fdf67be9b0070e0c5e0730f9e7744013c32d6e0fc77fcf298f8ba74/pythonnet/pythonnet/discussions/2424
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/fda5c1020fdf67be9b0070e0c5e0730f9e7744013c32d6e0fc77fcf298f8ba74/pythonnet/pythonnet/discussions/2424
og:image:altThe 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:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
hostnamegithub.com
expected-hostnamegithub.com
Nonefdfdce9cd4f6ab85dca2b0d11264270829297c962dd5a79df449062d7822258f
turbo-cache-controlno-preview
go-importgithub.com/pythonnet/pythonnet git https://github.com/pythonnet/pythonnet.git
octolytics-dimension-user_id6050430
octolytics-dimension-user_loginpythonnet
octolytics-dimension-repository_id14748123
octolytics-dimension-repository_nwopythonnet/pythonnet
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id14748123
octolytics-dimension-repository_network_root_nwopythonnet/pythonnet
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release51c736e60b302bd039c9d5164573d176ceb24bb2
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/pythonnet/pythonnet/discussions/2424#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpythonnet%2Fpythonnet%2Fdiscussions%2F2424
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub SparkBuild and deploy intelligent appshttps://github.com/features/spark
GitHub ModelsManage and compare promptshttps://github.com/features/models
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
GitHub SponsorsFund open source developershttps://github.com/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/accelerator
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpythonnet%2Fpythonnet%2Fdiscussions%2F2424
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fdiscussions_fragments%2Fdiscussion_layout&source=header-repo&source_repo=pythonnet%2Fpythonnet
Reloadhttps://github.com/pythonnet/pythonnet/discussions/2424
Reloadhttps://github.com/pythonnet/pythonnet/discussions/2424
Reloadhttps://github.com/pythonnet/pythonnet/discussions/2424
pythonnet https://github.com/pythonnet
pythonnethttps://github.com/pythonnet/pythonnet
Notifications https://github.com/login?return_to=%2Fpythonnet%2Fpythonnet
Fork 770 https://github.com/login?return_to=%2Fpythonnet%2Fpythonnet
Star 5.4k https://github.com/login?return_to=%2Fpythonnet%2Fpythonnet
Code https://github.com/pythonnet/pythonnet
Issues 156 https://github.com/pythonnet/pythonnet/issues
Pull requests 18 https://github.com/pythonnet/pythonnet/pulls
Discussions https://github.com/pythonnet/pythonnet/discussions
Actions https://github.com/pythonnet/pythonnet/actions
Projects 0 https://github.com/pythonnet/pythonnet/projects
Wiki https://github.com/pythonnet/pythonnet/wiki
Security Uh oh! There was an error while loading. Please reload this page. https://github.com/pythonnet/pythonnet/security
Please reload this pagehttps://github.com/pythonnet/pythonnet/discussions/2424
Insights https://github.com/pythonnet/pythonnet/pulse
Code https://github.com/pythonnet/pythonnet
Issues https://github.com/pythonnet/pythonnet/issues
Pull requests https://github.com/pythonnet/pythonnet/pulls
Discussions https://github.com/pythonnet/pythonnet/discussions
Actions https://github.com/pythonnet/pythonnet/actions
Projects https://github.com/pythonnet/pythonnet/projects
Wiki https://github.com/pythonnet/pythonnet/wiki
Security https://github.com/pythonnet/pythonnet/security
Insights https://github.com/pythonnet/pythonnet/pulse
Answered https://github.com/pythonnet/pythonnet/discussions/2424#discussioncomment-11252913
DareDevilDenishttps://github.com/DareDevilDenis
DareDevilDenis https://github.com/DareDevilDenis
Q&Ahttps://github.com/pythonnet/pythonnet/discussions/categories/q-a
Calling long running C# from Python: Do I really need to call PythonEngine.BeginAllowThreads() ? https://github.com/pythonnet/pythonnet/discussions/2424#top
DareDevilDenis https://github.com/DareDevilDenis
Answered https://github.com/pythonnet/pythonnet/discussions/2424#discussioncomment-11252913
DareDevilDenishttps://github.com/DareDevilDenis
Return to tophttps://github.com/pythonnet/pythonnet/discussions/2424#top
Please reload this pagehttps://github.com/pythonnet/pythonnet/discussions/2424
https://github.com/pythonnet/pythonnet/discussions/2424
DareDevilDenis https://github.com/DareDevilDenis
Jul 30, 2024 https://github.com/pythonnet/pythonnet/discussions/2424#discussion-6994198
https://github.com/pythonnet/pythonnet/wiki/Threadinghttps://github.com/pythonnet/pythonnet/wiki/Threading
https://private-user-images.githubusercontent.com/87706793/353415338-0245eaa9-78d8-4cff-bd8d-75c005620637.png?jwt=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3NjkwNTU5NTQsIm5iZiI6MTc2OTA1NTY1NCwicGF0aCI6Ii84NzcwNjc5My8zNTM0MTUzMzgtMDI0NWVhYTktNzhkOC00Y2ZmLWJkOGQtNzVjMDA1NjIwNjM3LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNjAxMjIlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjYwMTIyVDA0MjA1NFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTY4OTU3ZmNiMGQ1ODBmZjhjMmUyMWQ2ZmY0YzVjMjNlMzYxZmI5NjZhMmZmMmFmOTM1Nzg1Y2E4NzgyYWMxNjgmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.NC-kLDUFv1SgjE7fkNvZtc5CfxybNSGEIbHTGTPly2I
PythonCallingDotNetThreadingTest.ziphttps://github.com/user-attachments/files/16426051/PythonCallingDotNetThreadingTest.zip
Give feedback.https://github.com/pythonnet/pythonnet/discussions/2424
DareDevilDenis https://github.com/DareDevilDenis
Nov 14, 2024 https://github.com/pythonnet/pythonnet/discussions/2424#discussioncomment-11252913
#2209 (reply in thread)https://github.com/pythonnet/pythonnet/discussions/2209#discussioncomment-11249186
@lostmsuhttps://github.com/lostmsu
View full answer https://github.com/pythonnet/pythonnet/discussions/2424#discussioncomment-11252913
Oldest https://github.com/pythonnet/pythonnet/discussions/2424?sort=old
Newest https://github.com/pythonnet/pythonnet/discussions/2424?sort=new
Top https://github.com/pythonnet/pythonnet/discussions/2424?sort=top
Please reload this pagehttps://github.com/pythonnet/pythonnet/discussions/2424
https://github.com/pythonnet/pythonnet/discussions/2424
DareDevilDenis https://github.com/DareDevilDenis
Nov 14, 2024 https://github.com/pythonnet/pythonnet/discussions/2424#discussioncomment-11252913
#2209 (reply in thread)https://github.com/pythonnet/pythonnet/discussions/2209#discussioncomment-11249186
@lostmsuhttps://github.com/lostmsu
Give feedback.https://github.com/pythonnet/pythonnet/discussions/2424
DareDevilDenishttps://github.com/DareDevilDenis
Sign up for freehttps://github.com/join?source=comment-repo
Sign in to commenthttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpythonnet%2Fpythonnet%2Fdiscussions%2F2424
🙏 Q&A https://github.com/pythonnet/pythonnet/discussions/categories/q-a
https://github.com/DareDevilDenis
https://github.com/pythonnet/pythonnet/discussions/2424
https://github.com/settings/replies?return_to=1
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.