Skip to content
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

Top level await for Node.js process #59

Open
ccmcintyre-kingsds opened this issue Jul 21, 2022 · 3 comments
Open

Top level await for Node.js process #59

ccmcintyre-kingsds opened this issue Jul 21, 2022 · 3 comments
Labels
enhancement New feature or request javascript Relates to JavaScript or Node.js code pipes Relates to interprocess streaming python Relates to Python code

Comments

@ccmcintyre-kingsds
Copy link
Contributor

Node.js supports await/async behaviour even in the top-level scope, either through the --harmony-top-level-await flag prior to Node.js 14.8, or for ES modules since Node.js 14.9. Qualifying as an ES module for the purpose of this feature is in principle achievable using .mjs file extensions, declaring "type": "module" in the relevant package.json file, or by passing an --input-type=module flag to the node process when eval-ing script strings. This lattermost option sounds most relevant to Bifrost, but evidence suggests that none of these flags are meaningful when passed to to the Node.js process initialization (via subprocess.Popen in python, at py_nodejs.py), which is setting up a process which will be called via streaming pipes and populated via shared memory, rather than passing scripts to be directly eval-ed by a new Node.js process.

Top-level await is a desirable feature to support for Bifrost broadly, if possible. When investigating solutions, please consider both the above caveats relating to the relevant flags, and also consider how solutions might impact or be impacted by the following behaviour of Bifrost's current script evaluation code, taken from main.js:

//evaluate some script in the given context.
//Note that we await in order to allow for scripts which end in a promise
//to fully complete
async evaluate(script){
    await vm.runInContext(script, this.context);
}

Whatever solution is implemented should be able to support the async initalization of dcp-client using top-level await.

@ccmcintyre-kingsds ccmcintyre-kingsds added enhancement New feature or request python Relates to Python code javascript Relates to JavaScript or Node.js code pipes Relates to interprocess streaming labels Jul 21, 2022
@wesgarland
Copy link

Remember also that ES6 modules are by definition strict-mode code, which is generally a good thing, but has observable differences from non-strict code.

@wesgarland
Copy link

Also - if you are executing the code programatically, it's trivially easy to wrap an async iife around it if that gets you what you want. And you can do async dcp-client init without top-level await; you can use regular promise syntax instead. dcp-client.init().then(run my code);

@ccmcintyre-kingsds
Copy link
Contributor Author

@wesgarland

Remember also that ES6 modules are by definition strict-mode code, which is generally a good thing, but has observable differences from non-strict code.

Thank you for the reminder; that will be good to keep in mind, and we probably should give some broader thought to how we treat and enforce (or don't enforce) strict mode in Bifrost.

Also - if you are executing the code programatically, it's trivially easy to wrap an async iife around it if that gets you what you want. And you can do async dcp-client init without top-level await; you can use regular promise syntax instead. dcp-client.init().then(run my code);

Fair, and nothing prevents us from doing so with dcp-client at this time. The top-level await feature still seems like something we want to support in general for people using Bifrost's everyday Python+Node.js interop, but it's certainly not blocking functionality for distributing workloads, if we need to push this feature down the road.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request javascript Relates to JavaScript or Node.js code pipes Relates to interprocess streaming python Relates to Python code
Projects
None yet
Development

No branches or pull requests

2 participants