The async REPL introduced in Python 3.8 is very nice for quick tests and experiments, supporting top-level "await" (see https://bugs.python.org/issue37028). I'm using it often when developing code that heavily relies on Python's asyncio module.
A drawback of the basic interface when launching it with "python -m asyncio" is that one usually still needs to enter a couple of statements to load the package you're working on, etc. To overcome this I've added a __main__.py to the mpyc package such that entering "python -m mpyc" will launch the async REPL like this:
asyncio REPL 3.10.0a2 (tags/v3.10.0a2:114ee5d, Nov 3 2020, 00:37:42) [MSC v.1927 64 bit (AMD64)] on win32
Use "await" directly instead of "asyncio.run()".
Type "help", "copyright", "credits" or "license" for more information.
>>> import asyncio
>>> from mpyc.runtime import mpc
>>> secint = mpc.SecInt()
>>> secfxp = mpc.SecFxp()
>>> secfld256 = mpc.SecFld(256)
>>>
This enables the async REPL but also a "preamble" with some additional code is executed.
To program mpyc.__main__.py, however, I basically had to copy asyncio.__main__.py and make a few changes throughout the code. It works alright, but to take advantage of future improvements to asyncio.__main__.py it would be very convenient if asyncio.__main__.py becomes reusable.
With the added feature for specifying a "preamble", programming something like mpyc.__main__.py requires just a few lines of code:
import asyncio.__main__
if __name__ == '__main__':
preamble = ('from mpyc.runtime import mpc',
'secint = mpc.SecInt()',
'secfxp = mpc.SecFxp()',
'secfld256 = mpc.SecFld(256)')
asyncio.__main__.main(preamble)
The attachment contains the current version of mpyc.__main__.py, which to a large extent duplicates code from asyncio.__main__.py. A couple of, I think, minor changes are applied to make the code reusable, and to add the preamble feature.
Would be nice if asyncio.__main__.py is updated in this manner in Python 3.10 such that package developers can tailor it to their needs?
|