Link Search Menu Expand Document

process

This module allows parallel processing by providing classes and functions that allows for spawning operating system processes thereby leveraging multiple processors on a machine.

Example Usage:

var shared = SharedValue()

var pr = Process(@(p, s) {
  echo 'It works!'
  echo p.id()
  s.set({name: 'Richard', age: 3.142})
}, shared)

pr.on_complete(||{
  echo shared.get()
})

pr.start()
echo 'It works fine!'
# pr.await()  # this can be used to wait for completion.
echo 'It works fine again!'

Output:

It works fine!
It works fine again!
It works!
75608
{name: Richard, age: 3.142}

Properties


process.cpu_count
The number of CPU cores available on the current device.
type number

Functions


process.process(fn: function [, shared: SharedValue])
Creates a new instance of Process for the function fn. This constructor accepts an optional SharedValue.

The function passed to a process must accept at least one parameter which will be passed the instance of the process itself and at most two parameters if the process was intitalized with a SharedValue.

Classes


class SharedValue


The SharedValue object allows the sharing of single value/state between processes and the main application or one another.

SharedValue supports the following types:

  • Boolean
  • Number
  • String
  • List
  • Dictionary

@note Lists and Dictionaries cannot be nested in a SharedValue.

class SharedValue methods


SharedValue()
constructor
lock()
Locks the SharedValue and disallows updating the value.
unlock()
Unlocks the SharedValue to allow for updating the value.
is_locked()
Returns true if the SharedValue is locked for updating or false otherwise.
return boolean
  • a SharedValue is locked if in an invalid state.
set(value: boolean | number | string | list | dictionary)
Sets the value of the SharedValue to the given value. It returns the number of bytes written or false if the SharedValue is in an invalid state.
return number | boolean
locked_set(value: boolean | number | string | list | dictionary)
Locks the SharedValue for writing then sets the value to the given value and unlocks it. It returns the number of bytes written or false if the SharedValue is in an invalid state.
return number | boolean
get()
Returns the value stored in the SharedValue or nil if no value has been set.
return any

class Process


This class allows creating and spawning operating system processes and using them to run functions.

class Process methods


Process(fn: function [, shared: SharedValue])
Creates a new instance of Process for the function fn. This constructor accepts an optional SharedValue.

The function passed to a process must accept at least one parameter which will be passed the instance of the process itself and at most two parameters if the process was intitalized with a SharedValue.

constructor
id()
Returns the ID of the process or -1 if the process is in an invalid state or has not been started.
return number
on_complete(fn: function)
Adds a new listener to be called when the process finishes execution.
start()
Starts/runs the process. This function returns true or false if the process is in an invalid state.
return boolean
await()
Awaits for the process to finish running and returns it’s exit code or -1 if the process is in an invalid state.
return number
is_alive()
Returns true if the process is running or false if not.
return boolean
kill()
Kills the running process. Returns true if the process was successfully killed or false otherwise.
return boolean

Back to top

Copyright © 2021 Ore Richard Muyiwa. Distributed under the MIT license.