在Transcrypt Python to JavaScript编译器中实现协同程序时,我遇到了以下奇怪的问题。
Transcrypt使用CPython 3.6的本机解析器生成AST。 对于异步全局函数defs,它会生成AsyncFunctionDef节点。 但对于异步方法,它没有! 然而,CPython本身似乎正确地编译了异步方法。
所以下面的一段代码与CPython一起运行,但是Transcrypt无法运行它,因为CPython的AST模块生成的AST似乎缺少方法的AsyncFunctionDef节点(而不是全局函数)。
所以下面的代码不会生成AsyncFunctionDef节点:
from org.transcrypt.stubs.browser import __pragma__, __envir__
# Note that CPython will ignore all pragma's
# Provide waitAWhile for Transcrypt
__pragma__ ('js', '{}', '''
function waitAWhile (aTime, asio) {
return new Promise (resolve => {
setTimeout (() => {
resolve (aTime);
}, 1000 * aTime);
});
}
''')
# Provide waitAWhile for CPython
__pragma__ ('skip') # Compile time, needed because import is done compile time
import asyncio
def waitAWhile (aTime, asio):
return asio.sleep (aTime)
__pragma__ ('noskip')
# Actual code to be tested
async def f (waw, asio):
print ('f0')
await waw (2, asio)
print ('f1')
class C:
def __init__ (self):
self.aTime = 2
async def g (self, waw, asio):
print ('g0')
await waw (self.aTime, asio)
print ('g1')
c = C ()
# Just call async functions for Transcrypt, since in the browser JavaScript is event driven by default
if __envir__.executor_name == __envir__.transpiler_name:
f (waitAWhile, None)
c.g (waitAWhile, None)
c.g (waitAWhile, None)
f (waitAWhile, None)
# Create event loop and tasks for CPython, since it isn't event driven by default
else:
eventLoop = asyncio.get_event_loop ()
tasks = [
eventLoop.create_task (f (waitAWhile, asyncio)),
eventLoop.create_task (c.g (waitAWhile, asyncio)),
eventLoop.create_task (c.g (waitAWhile, asyncio)),
eventLoop.create_task (f (waitAWhile, asyncio)),
]
waitingTasks = asyncio.wait (tasks)
eventLoop.run_until_complete (waitingTasks)
eventLoop.close ()
我错过了什么?异步方法是官方支持的,不是吗? 在PEP 492中找不到任何具体内容。
示例的完整代码是:
mysql_fetch_assoc()
答案 0 :(得分:1)
Eventually I got the parser to work properly.
I must have blocked the parse somewhere else initially.
Probably I forgot to call visit
from a node higher up the tree.
Unfortunaly I can't reproduce the problem anymore.
For those interested, the parser code is at:
https://github.com/QQuick/Transcrypt/blob/master/transcrypt/modules/org/transcrypt/compiler.py
line 2045.
Most important: Python's ast module works fine, although it could do with a bit more documentation.
There's a (quite compact but useable) 3rd party doc at: