Is there a way to call run() multiple time without load and compile xxx.wasm again ?

30 views
Skip to first unread message

Kyo Lee

unread,
Jun 21, 2019, 1:05:52 PM6/21/19
to emscripten-discuss
Hi, it's amazing for emscripten build c/c++ program to wasm, and run at a website !

Recently, I try to emcc build ffprobe.bc to ffprobe.wasm, there output two file : ffprobe.js and ffprobe.wasm

the ffprobe.js is a bridge to fetch and compile ffprobe.wasm, and run it by WebAssembly Api at browsers Web Worker

I use emcc --pre-js --post-js to set my custom change in ffprobe.js

emcc \
  -s EXIT_RUNTIME=0 \
  -s ENVIRONMENT=worker \
  -s WASM=1 \
  --pre-js pre.js \
  --post-js post.js \
  -o ffprobe.js \
  ffprobe.bc && \

My Question Below:

If I wrapped the compiled code by a function, and work it on Web Worker, it works good.

But when run the function multiple, fetch and init ffprobe.wasm will call multiple. (its slow...)

I try to find a way to just re run the compiled binary.

So I change the code,to re run() multiple,below is what I do at my Web Worker onmessage function :

self.onmessage = function (e) {
    - set noIntialRun: true
    - set Module['calledRun'] = false
    - set shouldRunNow = true
    - call Module['run'](e.data.arguments)
}

> -------------------------------------------------------------- <

The result is

    - if i just call arguments = ["-version"], it works well at any times I call the function
    - if i do the FS usage,like arguments = ["-v", "error", "-select_streams", "v:0", "-show_entries", "stream=width,height", "-of", "csv=s=x:p=0", "/my_data/test_1.flv"],this output the video metainfo width and height. it works well at first call, and the second time , stderr: Argument '/my_data/test2.flv' provided as input filename, but '/my_data/test1.flv' was already specified.

    The error reason is:
       when call run(arguments) at the second time, the arguments have double length (the old args concat the new args), just like "ffprobe -version -version" ("-version -version" command can work well. but the FS usage double length will throw the error -- same behavior at terminal when ffprobe double -i file) 

is there a way to "RESET arguments" ?
can someone help me ... thx all !

J Decker

unread,
Jun 21, 2019, 2:36:07 PM6/21/19
to emscripten-discuss
What this sounds like, is ffprobe was assuming to run once itself, so it leaves variables set in its heap, and other global state variables.
You'd probably have to introduce something to reload the initial memory state... or modify the program to not assume variables are 0 initialized....


--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/emscripten-discuss/be85ecb3-2b97-419d-b0ea-0f6f3414beae%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alon Zakai

unread,
Jun 21, 2019, 7:07:52 PM6/21/19
to emscripte...@googlegroups.com
The MODULARIZE option can help with this, it puts everything in a function for you. Each time you call that function you get a completely new instance of everything, that is safe to call.

Another option is to turn the code into a library - without a main() function (that is special and called automatically, and has arguments, etc.), and instead some other functions are exported that you call. Then you can handle global state in them in a way that makes sense.

On Fri, Jun 21, 2019 at 10:05 AM Kyo Lee <rm2048...@gmail.com> wrote:
--
Reply all
Reply to author
Forward
0 new messages