-
Notifications
You must be signed in to change notification settings - Fork 747
.NET Core support and CoreCLR embedding - cross-platform API #96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Note that for .NET 4+ still have to use unmanaged exports. |
apparently @lstratman has extensive experience porting Mono to CoreCLR embedding. |
Thanks @denfromufa :). It's funny, I actually had a fork of this repo from a while back with the goal of looking into this. But, since my company has only a single customer that might want to integrate our .NET server application into their Python application on Linux and, even then, only at some point in the future, it's down towards the bottom of my priority list for development unfortunately. However, I'm happy to offer some technical guidance. I implemented basically this same functionality for Node.js for the Edge.js project and a lot of the code that I wrote there can probably be re-used or easily re-purposed for pythonnet. https://github.com/tjanczuk/edge/blob/master/src/CoreCLREmbedding/coreclrembedding.cpp#L192 contains the code that spins up the .NET Core CLR in any process. Towards the end of the method (https://github.com/tjanczuk/edge/blob/master/src/CoreCLREmbedding/coreclrembedding.cpp#L673), we get pointers to a bunch of .NET methods that serve as the managed end of the Node.js->.NET interop process. These function pointers are then invoked anytime that we need to call from Node.js to .NET. Implementations of those .NET methods are in https://github.com/tjanczuk/edge/blob/master/src/double/Edge.js/dotnetcore/coreclrembedding.cs and are included in the .NET Core application that's run from within Edge.js through a NuGet package that needs to be referenced (see https://github.com/tjanczuk/edge#how-to-specify-additional-clr-assembly-references-in-c-code). This works well for Edge.js because the interaction between Node.js and .NET is all encapsulated in functions in one language or the other. So to call between the two domains, you're always invoking functions, never creating variables, setting properties, etc. However for pythonnet, we're obviously more "integrated" into the Python syntax, so we can do things like create variables, set properties, etc. You can write more methods in the .NET end of the interop process to facilitate this, or try to stay in unmanaged code as much as possible by calling the .NET Core CLR native methods to do things like create variables and set properties. That would be the more performant, but potentially more difficult, route. This will have to be done through the ICLRRuntimeHost instance that you get back from calling coreclr_initialize(). It should theoretically be possible, but is left as an exercise to the interested reader. Anyway, that concludes my brain dump, I hope that it helps. Feel free to post questions and I'll do my best to answer them. At some point, I may have some time for actual development as well, but can't offer any promises. |
@lstratman thank you for very detailed response, there is a question from MS if CoreCLR C-API is good enough, would you mind replying there? MS is considering standardizing C-API for .NET ecosystem. https://blogs.msdn.microsoft.com/dotnet/2016/09/26/introducing-net-standard/#comment-138395 Are the four methods (Initialize, Shutdown, Create_Delegate, Execute_Assembly), mentioned enough for embedding CoreCLR in pythonnet? |
I've replied to that thread that those methods are sufficient. Looking further into the existing pythonnet code, I realized that it does all of the things that I mentioned earlier (create variables, set properties, index arrays, etc.) in managed code anyway, so once the "spinning up CoreCLR" code is integrated from Edge.js, pythonnet should just be able to re-use a lot of its existing codebase. |
Another thing holding of CoreCLR port is Reflection.Emit used in pythonnet in one place: https://github.com/dotnet/corefx/issues/4491#issuecomment-189756092 But we could possibly just do conditional compilation for this feature. |
otherwise try ikvm.reflection.emit or mono.cecil |
Hi, I started work toward Python.Net Core edition in this fork https://github.com/var1ap/pythonnet , stay tuned. |
@var1ap this is great news, do you agree to transition from Zope to MIT license, if your port is successful? |
@denfromufa of course agree. |
Greate news! |
Also pythonnet successfully ported to dotnet core 1.1(Linux x64 Ubuntu 16.04) (in the same fork). |
Hi guys! Pythonnet really works under dotnet core >= 1.0(NetStandard1.5). Thank you very much var1ap for your research. I continued to work on porting pythonnet to dotnet core in this repo https://github.com/dmitriyse/pythonnet/tree/coreclr. Also I plan to improve my binary distribution and avoid any dll load magic. Finally we should get good nuget based distribution. |
@var1ap @dmitriyse can you prepare a pull request with compilation flags for coreclr? much like .NET/Mono flags that we have currently. |
I can spend time to reapply coreclr code adoption in the next week. Please consider with vmuriart, which fork/branch is most suitable for this work, where I should fork. |
@dmitriyse on top of @denfromufa, after reviewing @dmitriyse and @var1ap, their changes look pretty straight forward to upgrade to Another issue is that Currently what I'm doing is slowly porting over some of the changes @dmitriyse and @var1ap did to minimize the changes for when we migrate to |
I agree with your comments @vmuriart
.NET 4.5 is a bummer, I'm also mostly on .NET 4.0 due to old Windows Server
editions.
I'm also against deprecating Mono, at least until CoreCLR works better.
…On Wed, Feb 22, 2017, 7:38 AM Victor Uriarte ***@***.***> wrote:
@dmitriyse <https://github.com/dmitriyse> on top of master, but at the
rate we are committing changes at the moment it wouldn't make sense to do
all the work if it won't get merged in immediately.
@denfromufa <https://github.com/denfromufa>, after reviewing @dmitriyse
<https://github.com/dmitriyse> and @var1ap <https://github.com/var1ap>,
their changes look pretty straight forward to upgrade to coreclr. My
hesitation is that it seems that it would require us to upgrade from 4.0
to 4.5 framework which may drop users (I still use 4.0). We could throw
in a bunch of #if directives, but it would make the code very messy.
(side note, We actually got rid of most mono_linux and mono_osx flags
from the code thanks to @dmitriyse <https://github.com/dmitriyse>)
Another issue is that coreclr is still undergoing changes wrt their build
system. They've gone from project.json back to an upgraded msbuild with
new project file formats. mono wouldn't compile against either of these
options; ie. we would need to have two sets of csproj/sln/packages.config
files in order to ensure mono can still be used for compiling (unless we
drop mono?)
Currently what I'm doing is slowly porting over some of the changes
@dmitriyse <https://github.com/dmitriyse> and @var1ap
<https://github.com/var1ap> did to minimize the changes for when we
migrate to coreclr. I'm hoping to find a clean way to maintain clr 4.0
and add coreclr, but at the moment I'm not finding it and it might need
to be part of pythonnet 3.0.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#96 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AHgZ5Y0zKAbdsaHCca6mjU__eMFuf9Syks5rfDprgaJpZM4F4h18>
.
|
Hi! I restarted work on porting PythonNet to CoreCLR. Now it can be performed as additional files. |
@dmitriyse this looks much cleaner! Can you open a pull request for review cycle? |
Ok, I will create pool request right after some useful point. I fixed 102 from 120 migration issues to get pythonnet working with at least one build configuration. |
Half of the problem solved - python can be embedded in to CoreCLR 2.0 (#519) System.Reflection.Emit works good under CoreCLR 2.0 except for CDecl calling convention. CDecl currently unsupported in the CoreCLR 2.0 As a workaround I changed XDecref/XIncref to Py_DecRef/, Py_IncRef. The rest part is to inject CoreCLR into python through CoreCLR C Api. |
@dmitriyse for cdecl did you mean this issue? https://github.com/dotnet/corefx/issues/9800 |
Yes, exactly this flaw. |
@dmitriyse for rest part to inject CoreCLR into Python we can avoid CoreCLR C API, if the DLLExport becomes stable with .NET Core, @3F has to use custom CoreCLR at the moment. DLLExport is the next generation of UnmanagedExports, that we are currently using in .NET Framework: |
@dmitriyse with your valid finalizer from #532 I'm getting this unexpected crash on .NET Core 2.0, which does not appear on .NET Framework:
|
@djoyce82 this is great! definitely, please submit a pull request. |
@djoyce82 That's great work, does it build and run from Linux? What commands did you use to build it and run the tests? |
|
@Cronan pass |
Thanks @denfromufa, I fixed it by declaring the variable outside the loop - there was a single other case of this:
|
@denfromufa @djoyce82 Lots of the tests failed, with this kind of thing:
|
If I want to use a .net core assembly in python and only care about using on windows, is that possible with the current python.net version? Thanks! |
@weixu02 this is possible via npython.exe if you compile the full pythonnet solution |
Let me document the current state of .net core support for the community:
Embedding means calling into Python from .NET (Python.Runtime.DLL) 1st "+|-|*" sign means supporting code is available "+" means available Outstanding issues and PRs in the order of priority: |
Hi, I'm trying to test calling from dotnet core into CPython.
Installing the source version with I also tried installing via nuget but got (for framework netcoreapp2.0)
Questions:
|
@MatthiasKauer current dotnet support is listed for master branch, mono should be installed according to official mono installation guide (see also .travis.yml), pip is primary channel for full installation, nuget support is limited right now, e.g. see #165. |
Sorry for my ignorance. I'm trying to get embedding to work. I've installed pythonnet from nuget (2.3.0-py27-dotnet). But this Python.Runtime.dll doesn't seem to be .Net Standard or -Core. Tried to compile the source code \pythonnet-master\src\runtime\Python.Runtime.csproj to Standard or Core but it seem to be a .Net 4.x. Also tried the Python.Runtime.dll in the Wheel package from here., same problem. |
@MrM40 .net core builds are in this solution: https://github.com/pythonnet/pythonnet/blob/master/pythonnet.15.sln But I recommend building from python side by passing the https://github.com/pythonnet/pythonnet/blob/master/appveyor.yml#L60 You can also grab the builds/wheels from appveyor artifacts. It is not possible to keep all 3 major runtimes (.NET Framework, .NET Core, and Mono) in the same solution/project. |
Still running against a wall. I only see release/debug to Mono and Win in those projects? |
Thanks :-)
I made two links: python2.7 and python27 in the same folder as Python.Runtime.dll pointing to "/usr/bin/python2.7". Not I got this error:
|
@MrM40 please don't sidetrack this .NET Core (CoreCLR) status with other issues. In a separate issue, document how you are building and how you are using pythonnet to make a reproducible case. Also search the issue tracker for duplicate issues before posting. |
I built https://github.com/pythonnet/pythonnet/blob/master/pythonnet.15.sln with the following compilation symbols:
Running this in a .NET Core application on Windows 8.1 causes the program to fatally crash:
Without any more information. I'm sure more information can be obtained, but I'm unsure how. Kind regards, Emil |
Please try UCS2 |
@dmitriyse Thanks! Changed to UCS2, now getting:
|
Unfortunately, I haven't tested this solution widely. Win 8.1 setup hasn't been tested. So you need to dive deeply with VS debugging. |
@dmitriyse Hah! I got it working :) I had this line hanging around:
Removing it fixed the issue. Even though I think the path is correct. |
Closed in favour of #984. |
Looks like with CoreCLR the unmanaged exports "hack" for embedding CLR can be extended by cross-platform hosting API:
https://github.com/dotnet/coreclr/issues/1256
This would allow simple C/C++ code to embed CLR
Current state of .NET Core support:
#96 (comment)
The text was updated successfully, but these errors were encountered: