-
-
Notifications
You must be signed in to change notification settings - Fork 518
[GSoC] Implement asyncio support #763
Comments
(Passing by here) I’m extremely interested how Windows-related stuff would be handled (if there are any… I’m not familiar how much of the Java standard library can be leveraged to handle platform-specific stuff). It would be super cool if you can pull it off. Some decisions might be required in order to decide what is public API and what is not; there are quite a lot of undocumented but public-looking things in there. |
@uranusjr Of the many problems with Java, cross platform is one of it's strengths. This is one project where there shouldn't be any major cross-platform issues; the behavior of networking etc in the Java standard library is consistent on all platforms Java supports. |
@BPYap This timeline looks really solid to me. 3 clear deliverables; the intermediate steps make sense, and the amount of time allocated to each step seem achievable (of course, the gods are now pointing and laughing at the humans making plans... 😄). The only pieces needed to make this a compelling final proposal are:
|
@freakboy3742 Thanks for your feedback! I finished the project description and added a risks section, let me know what you think 😄 |
@BPYap Honestly - this is looking pretty solid. You could probably submit it as-is and it would be a very competitive proposal. The only suggestion I would have about making it stronger would be to discuss which parts will need to be built from scratch in Java, and which parts of Python's existing asyncio code can be re-used and cross-compiled. Are you going to have to build the entire event loop yourself? Or is the work going to be mostly debugging VOC so that we can compile the asyncio Python code? |
@freakboy3742 I did some research this afternoon and have identified parts of asyncio library that I think could be reused:
For the above modules, I will be focusing on debugging voc to support their cross compilation during the weeks assigned for them. For event loop, I plan to rewrite it in Java, as the module Some examples:
|
@BPYap Sure - that all makes sense. If you can integrate those details into the main proposal, that would be awesome. One additional clarification: What about the socket layer? Are you going to be able to use Java's networking layer directly, or will you need a third party library? |
Updated proposal. 😄 @freakboy3742 It seems Java socket class does not support Unix domain address family. Support for it requires a third party library. I'm thinking to support only the "generic" socket operation and operations specific to unix (i.e |
@BPYap Ok - this is probably ready for you to submit as a final proposal to the GSoC website. I'm sure we'll continue to fine tune it if you get selected for the SoC, but you've definitely shown enough detail here to warrant solid consideration in the selection process. |
This project was selected for the 2018 GSoC. |
Week 1 Update ## (Temporary on hold due to schedule change)GoalComplete asyncio.coroutines implementations in voc [#788 Work in Progress] Relevant source codesSummaryWhat's a coroutine in asyncio? A coroutine can be defined in 2 ways:
Generators defined as coroutines can do cool stuffs like ** Besides coroutine, Ascyncio implementation in CPython CPython defines coroutine as a Tasks ChecklistOver the next couple days, I will complete the tasks below:
More technical details will be added soon, stay tuned :-) |
Week 2 UpdateGoalUnderstands and implements exception related to asyncio [#817] Relevant source codesSummaryThere are 4 new Exception classes that are related to asyncio module and are not a part of Python built-in Exceptions:
When does the errors occur?
Implementation Tasks Checklist
|
I have what seems to be a particularly nasty example that is peddling some futures around in a custom event loop so I can iteratively tick through the coroutines. It's mimicking what I ultimately want to do embedding Python with Unity3d (with a .net transpiler) . In my situation, much of the code would be calling into .net Unity3d stuff, but I've approximated it in pure Python. It imploded when I tried to run it but I don't think anybody would have been surprised. I assume I should just sit on it for a little bit but let me know if there's a spot where you'd like me to dump it. I can jazz it up for wider consumption in some little github project. |
Hi @rockobonaparte , I'm currently working on bringing in features specified in PEP 342 and PEP 380 to voc and it will take a while before I start event loop implementation. However, I’d be interested to know your approach. If you don't mind, could you explain it in higher level of details on how you are embedding Python coroutines with .net Unity3d? Sorry for late reply, as I check in here once in a week to update my progress. Cheers 😄 |
Week 3 UpdateGoalComplete generator features required by coroutine [#821, #823, #831] Relevant resourceSummaryPEP 342: Coroutines via Enhanced Generators
The syntax adjustments mentioned above are:
New generator methods:
Implementation in voc In PEP 380: Syntax for Delegating to a Subgenerator
In Implementation in voc Tasks Checklist
|
@BPYap The current plan is to extend the voc to transpile to MSIL (Microsoft Intermediate Language)--or particularly the bytecode-compiled target version of that, since that's what the .net runtime uses. Unity 3d is technically using Mono, which means it's also running off of that. This would let me compile Python to .net and give me a fighting chance at Unity just flat-out recognizing Python as any old code you could feed it. IronPython is a dead end since the last mature version of it only works with Python 2.7, and the 3.0 branch can't import asyncio at all. Unity also wouldn't directly recognize it as something it can use; I'd have to subclass MonoBehaviour and put a bunch of overhead in place. Since the coroutines are what I'm after, it's a no-go. My reason for a custom scheduler is so I can manually tick it instead of delegating control of my thread to it. Unity has to keep running the engine loop and can't surrender itself to a blocking coroutine scheduler. I'd treat the coroutine scheduler as a subsystem and it would just get ticked at least every frame. I could add smarts as necessary if this isn't enough and I need to schedule more intelligently. I'm very much all-in on coroutines because there's a lot of sequential game logic that is written much more concisely with coroutines than, say, having to drag a bunch of game state around to make some body of code pseudo-reentrant. Unity's built-in technique for this are kind of gross; it relies IEnumerators, which would be akin to writing coroutines just using old-school yield statements in Python. Anyways you don't want to be waiting on me here. I just got as far as discovering the visitor code in voc.python.ast and I see I have my work cut out for me. I'm currently trying to collapse some of that code into a common helper to reduce the FUD of trying to write a .net equivalent of ~2000 lines of code in one file. |
Week 4 & 5 UpdateGoalSupport raising and catching of custom exception [#847] Relevant resourceSummaryCurrently voc only recognizes built-in Python exceptions defined in In the case of asyncio modules, here are the files and reasons that caused transpilation error:
Since all of the errors are related to custom exceptions, solving the custom exceptions problem in voc should result in successful compilation of entire asyncio module in voc, which means we probably won't have to implement asyncio module in Java. Implementation in voc Tasks Checklist
|
Week 6 & 7 UpdateGoalInvestigating and fixing bugs occurred when importing asyncio module through voc [#849, #854] Relevant resourceSummaryAside from custom exception importing, the other two problems causing Issue 1 *Side notes: While solving this issue, I encountered two different bugs related to Implementation in voc Issue 2 In summary, the Implementation in voc
Tasks Checklist
|
Week 8 UpdateGoalSolve regression issues in #854 Relevant resourceSummaryThis week mainly focus in re-implementing Keeping track of nonlocalsTo keep track of which variables are When
** owner context refers to the enclosing context (scope) that owns the nonlocal variable. Determining owner contextTo load/store
** Communicating between different contextWhen we have pinpointed owner context for our nonlocal variable, we need a way to load/store from the owner context. At first glance, it seems calling Therefore a 'middle-man' lookup table is required to exchange variable's value between inner scope and outer scope. Under To load nonlocals and closure variables (for method and generator) from inner context, we can retrieve its value from Tasks Checklist
|
Week 10 & 11 UpdateGoalContinue nonlocal implementation and finding out which modules are causing issues when importing SummaryThis week we are trying out new way to implement nonlocal and closures by passsing in locals of parent context as reference into closure object and implementing several data structures from The issue with extra variables to keep track of nonlocalsPreviously we are using a field variable in Fortunately, there is this Issues when importing asyncio moduleWhen Tasks Checklist
|
Week 12 UpdateWell, that's a wrap for GSoC 2018. 😄 Huge thanks to @freakboy3742 and BeeWare project for an amazing GSoC experience. For summary of what I did in GSoC 2018, visit https://pybee.org/news/buzz/2018-google-summer-of-code-final-report-yap-boon-peng/. |
Project Description
Python 3.4 introduced asynchronous I/O support to the Python core language. Python asyncio module provides infrastructure for writing single-threaded concurrent code using coroutines, multiplexing I/O access over sockets and other resources, running network clients and servers, and other related primitives.
This project focus on implementing asyncio’s coroutine and event loop (both timer & socket based) for voc in JVM. In order to achieve the end goal, base infrastructure such as coroutine class and specific statement such as ‘yield from’ would be implemented during first phase of the project and subsequent phases will focus in implementations of Python's asyncio library as Java API.
Successful implementation of asyncio in voc will greatly benefit toga_android development as event loop in Android API can be integrated in toga using the implemented asyncio library (similar to rubicon’s event loop for ios devices). Other than that, applications that utilize concurrent computing can be executed in Java Virtual Machine - users will be able to write concurrent functions (using asyc def/await in Python) and executes them in Java Virtual Machine after transpilation.
Testing, debugging, and documenting are integral parts of the project development. Therefore, they will be carried out continuously as the project progress. The project is broken down into three phases, with end of each phase corresponding to mentor’s evaluation.
Project Details
The following Python statements will be implemented through the ast module (i.e implements the conversion of ast nodes to Java bytecode):
yield from
async
await
Some modules can be directly compiled through voc with some minor adjustments and debugging on voc, hence I will focus on debugging voc transpiler during the weeks allocated for the following modules:
asyncio.base_futures
asyncio.futures
asyncio.base_tasks
asyncio.tasks
The event loop (both timer and socket based) will be rewritten in Java, as the module
base_events.py
depends on too many stdlib modules in which voc currently does not support, hence providing supports for all of them may not be feasible within my allocated timeline. Here's how some functionality in Python can be translated to Java:collections.deque()
--> Java'sDeque Interface
time.get_clock_info('monotonic')
--> Java'sSystem.nanotime()
socket.py
--> Java.net'sSocket
(prioritizes operations required by event loop, such as setup connection, receiving and sending data operations) **** Java does not support Unix Domain Socket natively, hence I'm planning to restrict socket's address family to IPv4 and IPv6
Working Timeline
Phase 1 (May 14 – Jun 16)
Aim
To build foundational infrastructure to support asyncio implementation
Deliverables
Coroutine class, Coroutine related Exception, yield from, async, await statement support
Main challenges
Dive into VOC internals and deal with low level Python/Java bytecode
Week 1 (May 14 - May 19)
Week 2 (May 21 - May 26)
Week 3 (May 28 - June 2)
Week 4 (Jun 4 - Jun 9)
Week 5 (Jun 11 - Jun 16)
Phase 2 (Jun 18 – July 14)
Aim
To implement event loop interface in Java virtual machine
Deliverables
asyncio task, asyncio queue, event loop interface
Main challenges
Figure out reusable Python implementations and how to compile them in voc and how to link them with pre-compiled binaries
Week 6 (Jun 25 - Jun 30)
Week 7 (July 2 - July 7)
Week 8 (July 9 - July 14)
Phase 3 (July 16 – August 14)
Aim
To implement event loop in Java virtual machine
Deliverables
Asynchronous timer, event loop, error handling API, Socket I/O support
Main challenges
Deal with networking and synchronization concept
Week 9 (July 16 - July 21)
Week 10 (July 23 - July 28)
Week 11 (July 30 - August 4)
Week 12 (August 6 - August 14)
Project Risks
Technical Risk
There’s possibility that technical difficulty will arise during implementations of asyncio for voc. I’ve compiled a list of tasks below that I think would cause timeline slips because I might underestimated the complexities of these tasks:
I will minimize the risk by preparing/do research earlier on these topics and communicate with mentors if I think that the tasks will be delayed.
In any event that delay to workflow is inevitable, I will still complete the project and fufill my responsibility even when GSoC ends. 😊
Extra Infomation
My name
Yap Boon Peng
Email
[email protected]
GitHub
https://github.com/BPYap
Education
Nanyang Technological University, Singapore (Computer Science Undergraduate, Year 2)
Timezone
UTC +8
Relevant Experience
Notes: I didn’t include community bonding period in working timeline above because I’ll be having final exams during the period. I will not be able to commit fully during that period but I can compensate by having the community bonding few weeks earlier before my finals (if mentors are ok with it) 😊
Cheers!
The text was updated successfully, but these errors were encountered: