A script language of time-sharing scheduling coroutine in single thread
This document introduces a set of functions about message queue. Message queue is only working on a coroutine group in the same thread.
There are two kinds of message: queue message and topic message.
Melang supports many coroutines to listen the same queue. If a message is a queue message, only one coroutine can obtain this message. If it is a topic message, every subscribed coroutine can obtain this message.
mq = Import('mq');
Subscribe a topic message.
mq.subscribe(qname);
Input:
qname - a queue name string.Return value:
nil.Error:
Unsubscribe a topic.
mq.unsubscribe(qname);
Input:
qname - a queue name string.Return value:
nil.Error:
Send message to a specified queue.
mq.send(qname, msg, asTopic);
Input:
qname - a queue name string.msg - the message that we want to send. The type of this argument must be integer, boolean, real or string.asTopic - if want to send a queue message, this argument must be false, otherwise true. This is an optional argument. Omitted means false.Return value:
nil.Error:
mq.recv(qname, timeout);
Input:
qname - a queue name string.timeout - a positive integer or nil. If it is a positive integer, program will wait for timeout microseconds. If it is zero, function returned immediately with nil as the return value. If it is omitted or a nil, prigram will wait until got a message.Return value:
nil on timeoutfalse on failureError:
//file a.mln
mq = Import('mq');
mq.send('test', 'hello');
//b.mln
mq = Import('mq');
sys = Import('sys');
msg = mq.recv('test');
sys.print(msg);
$ melang a.mln b.mln
The output is:
hello