All Projects → mrousavy → React Native Multithreading

mrousavy / React Native Multithreading

Licence: mit
🧵 Fast and easy multithreading for React Native using JSI

Programming Languages

typescript
32286 projects
js
455 projects

Projects that are alternatives of or similar to React Native Multithreading

Thread
type safe multi-threading made easier
Stars: ✭ 34 (-79.27%)
Mutual labels:  thread, multithreading, threading
Microjob
A tiny wrapper for turning Node.js worker threads into easy-to-use routines for heavy CPU loads.
Stars: ✭ 1,985 (+1110.37%)
Mutual labels:  multithreading, thread, threading
ObviousAwait
🧵 Expressive aliases to ConfigureAwait(true) and ConfigureAwait(false)
Stars: ✭ 55 (-66.46%)
Mutual labels:  thread, multithreading, threading
Thread Loader
Runs the following loaders in a worker pool
Stars: ✭ 945 (+476.22%)
Mutual labels:  multithreading, thread
Swiftcoroutine
Swift coroutines for iOS, macOS and Linux.
Stars: ✭ 690 (+320.73%)
Mutual labels:  multithreading, thread
Interlace
Easily turn single threaded command line applications into a fast, multi-threaded application with CIDR and glob support.
Stars: ✭ 760 (+363.41%)
Mutual labels:  multithreading, thread
Pebble
Multi threading and processing eye-candy.
Stars: ✭ 276 (+68.29%)
Mutual labels:  threading, multiprocessing
Archive Password Cracker
设计精良的压缩包密码破解工具,具有自定义字典、导出字典、选择字典等功能。基于Python实现,支持多线程与多进程,不断完善中……
Stars: ✭ 65 (-60.37%)
Mutual labels:  multithreading, multiprocessing
Enkits
A permissively licensed C and C++ Task Scheduler for creating parallel programs. Requires C++11 support.
Stars: ✭ 962 (+486.59%)
Mutual labels:  multithreading, thread
Pht
A new threading extension for PHP
Stars: ✭ 175 (+6.71%)
Mutual labels:  multithreading, threading
Ti.worker
Use Multi-Threading / Worker Threads in Appcelerator Titanium.
Stars: ✭ 95 (-42.07%)
Mutual labels:  multithreading, native
Hamsters.js
100% Vanilla Javascript Multithreading & Parallel Execution Library
Stars: ✭ 517 (+215.24%)
Mutual labels:  multithreading, thread
Concurrencpp
Modern concurrency for C++. Tasks, executors, timers and C++20 coroutines to rule them all
Stars: ✭ 340 (+107.32%)
Mutual labels:  multithreading, threading
Autooffload.jl
Automatic GPU, TPU, FPGA, Xeon Phi, Multithreaded, Distributed, etc. offloading for scientific machine learning (SciML) and differential equations
Stars: ✭ 21 (-87.2%)
Mutual labels:  multithreading, multiprocessing
Crypto Rl
Deep Reinforcement Learning toolkit: record and replay cryptocurrency limit order book data & train a DDQN agent
Stars: ✭ 328 (+100%)
Mutual labels:  multithreading, multiprocessing
Pelagia
Automatic parallelization (lock-free multithreading thread) tool developed by Surparallel Open Source.Pelagia is embedded key value database that implements a small, fast, high-reliability on ANSI C.
Stars: ✭ 1,132 (+590.24%)
Mutual labels:  multithreading, thread
Tutorials
机器学习相关教程
Stars: ✭ 9,616 (+5763.41%)
Mutual labels:  threading, multiprocessing
Java Concurrency Examples
Java Concurrency/Multithreading Tutorial with Examples for Dummies
Stars: ✭ 173 (+5.49%)
Mutual labels:  multithreading, thread
awesome-dotnet-async
A curated list of awesome articles and resources to learning and practicing about async, threading, and channels in .Net platform. 😉
Stars: ✭ 84 (-48.78%)
Mutual labels:  thread, threading
Pypette
Ridiculously simple flow controller for building complex pipelines
Stars: ✭ 258 (+57.32%)
Mutual labels:  multithreading, multiprocessing

react-native-multithreading

🧵 Fast and easy multithreading for React Native using JSI.


Buy Me a Coffee at ko-fi.com


Installation

npm install react-native-multithreading
npx pod-install

Requires a version of react-native-reanimated which includes PR #1790. Either patch it yourself, or wait until that lands in a release.

⚠️ Warning: This is still just a proof of concept - do not use this library in production! ⚠️

Why

Since JSI is becoming more mainstream, there might be functions that are actually blocking and take a while to execute. For example, a storage library like my react-native-mmkv or an SQLite JSI library might take a few milliseconds to execute a complex call. You don't want your entire React-JS thread to freeze when doing that, since users will perceive a noticeable lag or freeze.

That's where react-native-multithreading comes in; you can simply off-load such expensive calculations/blocking calls to a separate thread with almost no overhead while your main React-JS thread can concentrate on running your app's business logic, respond to user input, update state and more. You can also run complex JS calculations such as the Fibonacci number, but that's probably a rare use-case.

Inspired by @karol-bisztyga's Multithreading PR for Reanimated

Usage

To try out the Fibonacci Example, clone the repo and run the following commands:

yarn bootstrap
cd example
yarn ios

See my tweet 🐦

Shoot and Forget

To simply perform an expensive calculation on another thread without caring about the result, use the spawnThread function:

// JS thread
spawnThread(() => {
  'worklet'
  // custom thread
  // expensive calculation
})
// JS thread

The React-JS Thread will continue execution while the custom thread will run the given function on a custom parallel runtime.

Await

Since spawnThread returns a Promise, you can also await the result. The React-JS Thread will not be blocked and will still be able to continue execution elsewhere (timers, callbacks, ...), while the custom thread runs the given function in a custom parallel runtime.

const result = await spawnThread(() => {
  'worklet'
  // expensive calculation
  return ...
})

Fibonacci

This example calculates the Fibonacci Number for the given input. This demonstrates expensive calculation, awaiting the result, as well as using values from "outside". (fibonacci function and input are captured into the new thread and therefore immutable.)

const fibonacci = (num: number): number => {
  'worklet'
  if (num <= 1) return 1
  return fibonacci(num - 1) + fibonacci(num - 2)
}

const input = 50
const result = await spawnThread(() => {
  'worklet'
  console.log(`calculating fibonacci for input: ${input} in JS-Runtime: ${global._LABEL}...`)
  const fib = fibonacci(input)
  console.log("finished calculating fibonacci!")
  return fib
})
console.log(`Fibonacci Result: ${result}`)

What's possible?

  • You can use variables from "outside" (e.g. state), but those will be immutable/frozen.
  • You can use functions from "outside" if they also contain the 'worklet' directive.
  • You can assign Reanimated Shared Values.
  • You can call native JSI functions ("Host Functions") from a JSI library, e.g. every function react-native-mmkv provides.
  • You can asynchronously dispatch calls to functions from "outside" using runOnJS from react-native-reanimated.

What's not possible?

  1. At the moment, only iOS is implemented. I cannot implement Android until react-native-reanimated gets published with source-code (no prebuilt .aar)
  2. Since the library uses JSI for synchronous native methods access, remote debugging (e.g. with Chrome) is no longer possible. Instead, you should use Flipper.
  3. All functions you are calling inside a custom thread, must be workletized to truly run on a separate thread. So add the 'worklet' directive at the top of every function you're calling in that thread (including the thread callback itself), and don't forget to install the Reanimated babel plugin.

License

MIT

Credits

Note: Technically this is not multithreading, but rather multiprocessing.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].