task_after v1.1.0 TaskAfter
Documentation for TaskAfter.
This is a library to call a function after a set delay.
It will have the normal variation of the EVM/BEAM system and the underlying OS, so give or take a few milliseconds, like ~12 for Windows.
This keeps an ordered list of tasks to run, it should scale decently, however if it gets too large then you may want to create more Workers to shard the tasks across, this is entirely in your control.
Summary
Functions
cancel_task_after
Functions
cancel_task_after
task_id -> A task id opts -> Can be:
name: name
|pid: pid
-> Specify a non-global task handler, if unspecified that the application:global_name
must be specifiedcall_timeout: timeout
-> Override the timeout on calling to theTaskAfter.Worker
no_return: true
-> Do not return the id or error, just try to register and forget results otherwiserun_result: pid
-> Sends the result of the task to the specified pid after running it as an async task while returning the Taskrun_result: :in_process
-> Runs the task in theTaskAfter.Worker
process to do internal work, do not use this, returns the value directly thoughrun_result: :async
-> Runs the task as an async task and dismisses the result while returning the Taskrun_result: nil
-> Default: Does not run the task now, just cancels it immediately, returns the callback function
Examples
iex> cb = fn -> 42 end
iex> {:ok, auto_id} = TaskAfter.task_after(500, cb)
iex> {:ok, ^cb} = TaskAfter.cancel_task_after(auto_id)
iex> is_function(cb, 0)
true
task_after
timeout_after_ms -> integer millisecond timeout callback -> The 0-argcallback function opts -> Can be:
name: name
|pid: pid
-> Specify a non-global task handler, if unspecified that the application:global_name
must be specifiedid: id
-> A unique id, if nil or unspecified then it is auto-generatedcall_timeout: timeout
-> Override the timeout on calling to theTaskAfter.Worker
no_return: true
-> Do not return the id or error, just try to register and forget results otherwisesend_result: pid
-> Sends the result of the task to the specified pid after running it as an async tasksend_result: :in_process
-> Runs the task in theTaskAfter.Worker
process to do internal work, do not use thissend_result: :async
-> Default: Runs the task as an async task and dismisses the result
Examples
iex> {:ok, _auto_id} = TaskAfter.task_after(500, fn -> 21 end)
iex> :ok
:ok
iex> {:ok, :myid} = TaskAfter.task_after(500, fn -> 42 end, send_result: self(), id: :myid)
iex> receive do m -> m after 5 -> :blah end
:blah
iex> receive do m -> m after 1000 -> :blah end
42