<galibert[m]>
looks like it's (almost?) impossible to write a script that can run in both an async and a non-async context
<galibert[m]>
you're not allowed to write await when you're not in a non-async function even if you never run the code
zyp[m] has joined #glasgow
<zyp[m]>
it's possible, but whether it's feasible/useful depends on what you're trying to do
<zyp[m]>
I've e.g. made a python RPC client with pluggable transports; if you give it an async transport, you get an async RPC API, otherwise you get a blocking RPC API
<zyp[m]>
the server side in C++ does the same thing; if you give it an async transport, you can write async handlers, otherwise handlers must be blocking
redstarcomrade has quit [Read error: Connection reset by peer]
be65da6e451238ff has quit [Quit: WeeChat 4.7.0]
<galibert[m]>
zyp: I want is kind of the way around. I have a script that runs a i2s capture in glasgow, which sends some midi stuff, captures, writes the output somewhere and quits. I'd like to have this script, when run outside glasgow, do the midi stuff and quit (no capture, obviously). Technically, in the second case, I choose a different midi port so that it goes to an emulator for comparison
<galibert[m]>
* of the other way around.
<galibert[m]>
when run from glasgow, you're in an async context. When run from the command line, you're not. But it looks like you're not allowed to writ code that even looks like you're in an async context when you're not, even if you never execute it
<galibert[m]>
s/writ/write/
<galibert[m]>
e.g. if False: await run() will throw an exception if you're not in an async function
<galibert[m]>
but you can't call an async function without awaiting-it
<galibert[m]>
and it doesn't look like you can await something without using the await keyword
<josHua[m]>
yes, you cannot await from a non async context because, under the hood, await actually causes the function you are in to return a promise and a continuation
<galibert[m]>
josHua: you missed the if False:
<galibert[m]>
you cannot write the keyword async in a non-async context even if you never, ever execute the statement it is in
<galibert[m]>
s/write/_write_/, s/async/await/
<josHua[m]>
that’s correct
<josHua[m]>
await is not a function, it is a structural transformation that happens at a keyword
<galibert[m]>
which is my problem. I'm not trying to run async stuff in a non-async context, I'm trying to do different things when I'm in an async context and when I'm not
<tpw_rules>
my solution is valid, i can explain it if you like. i don't know if that implies you should use it
<josHua[m]>
if await even exists in a function, the function must return a promise no matter what
<josHua[m]>
can you be more specific about what exactly you are trying to do? like what call sites you expect to be used from, and how you wish to behave in each case?
<galibert[m]>
I expect the script to be used either from glasgow script mumble.py mu or on the command line python mumble.py
<galibert[m]>
(mu being my applet)
<josHua[m]>
how about having the main case in mumble.py start up an asyncio event loop and call into your async functions, then?
<josHua[m]>
s/main/`__main__`/
<galibert[m]>
there are a number of tables that are essentially my experiment definition in the script that I want to use in both cases, sending to the real hardware in one case and to an emulator in the other
<galibert[m]>
josHua: I tried. I realized I couldn't call the async functions from glasgow, because that requires await which is forbidden when not in glasgow
<josHua[m]>
I’m not sure I understand actually what you’re trying to implement. can you pastebin a sketch of the thing that doesn’t work?
<galibert[m]>
tpw: your solution somehow doesn't work, the function doesn't run
<galibert[m]>
josHua: the problem is around like 93, the if real: there
<galibert[m]>
real = in glasgow = in async context
<galibert[m]>
I found no way to run the run function
<josHua[m]>
I see
<galibert[m]>
which still allows me to reach the else: when not in an async context
<josHua[m]>
ok, so what you want is to create an asyncio context of your own to run the run function
<galibert[m]>
you can't create an asyncio context when you're alrady in one
<josHua[m]>
that's correct, but *that* you can do dynamically
<josHua[m]>
decide dynamically, even
<galibert[m]>
oh?
<galibert[m]>
so, I'm in the if real:, I know I'm in an async context, I want to run the run function, but I'm not allowed to use await. What should I do?
<josHua[m]>
that API might be a footgun after all. you might have to have two files for this -- one glasgow entry point, and one non-glasgow entry point.
<galibert[m]>
so I end up with three files, great
<galibert[m]>
(the entire point being to share the experiment setup)
<josHua[m]>
I think this is arguably a glasgow API bug, and `script`s should optionally have an `async def __glasgow_main__():` or something that they expose into the global namespace that `run_applet` will try to run after first `eval`'ing the script