Achieve multi threading in WASM for making http requests (using reqwest crate)

190 views Asked by At

I am trying to convert current implementation to WASM (for cloudflare workers).

But I have been facing problem with current implementation is that it uses multi threading to make requests, but I am unable to compile that code for WASM, so currently I am using async_std::spwan_local to make requests (source) which forces the program to execute each request on single thread. Is there anyway that I can achieve multi threading with the same?

Here is a minimal example I made to reproduce the error (further steps are in readme): https://github.com/ssddOnTop/cf_workers

1

There are 1 answers

2
kmdreko On

No, you cannot achieve multi-threading in WASM as a Cloudflare worker. It is not supported.

The Cloudflare Worker API documentation on WebAssembly says:

Threading

Threading is not possible in Workers. Each Worker runs in a single thread, and the Web Worker API is not supported.


For additional context. WebAssembly does not (yet) natively support threads. The way multi-threading can be done currently is by jumping through the JavaScript Web Workers API. As such, Rust code targeting WebAssembly cannot be multi-threaded (i.e. using std::thread::* APIs will fail). If this were for a website, the Web Worker API would be a option, but you'd have to navigate that manually. But since Cloudflare does not provide the Web Worker API to its workers, then that option is not available either.

I'm also skeptical that your design had any merit in the first place; a thread-per-request model is not known to be particularly efficient.