berryMQ logo

Python API

Decorators

berrymq.following(identifier[, guard_condition])

This is a decorator function. This decorator can use for only instanse methods of the classes which use Follower metaclass.

Parameters:
  • identifier (str) – receive message filter Identifier.
  • guard_condition (callable object with one paramater(message object)) – if this guard function return False, message receive will be skipped

See also

class Follower, function following_function()

berrymq.following_function(identifier[, guard_condition])

This is a decorator function. This decorator can use for only class methods, static methods, functions. if you want to use for instance methods, use following() instead of this function.

Parameters:
  • identifier (str) – receive message filter Identifier.
  • guard_condition (callable object with one paramater(message object)) – if this guard function return False, message receive will be skipped

See also

function following_function()

berrymq.auto_twitter(identifier[, entry[, exit]])

This is a decorator function. The decorated function is called, automatically sends message.

@auto_twitter("function")
def sample_function():
    ... do something

If this function is called, function:entry message will be sent before running function, and then function:exit message will be sent after running function.

Use entry, exit parameter for controlling message.

entry exit action
False False send both.
False True exit message only
True False entry message only
True True send both.
Parameters:
  • identifier (str) – Send message :ref:identifier
  • entry (True or False) – Send message before calling function?(default: False)
  • exit (True or False) – Send message after calling function?(default: False)
berrymq.twitter_exception()
This is a decorator function. If any exception raised in decorated function, send message.

Functions

Send Message

berrymq.twitter(identifier[, ...])
Send message. It is the most important function in berryMQ. This funciton can recieve any args and kwargs. These values are delivered via Message object.
berrymq.talk(identifier, callback_identifier[, ...])

Send message like twitter() method. This function has callback identifer. All receivers call back to this Identifier.

Note

This is planning feature. It isn’t implemented yet.

berrymq.send_message(token, identifier[, ...])

Send message to other node. This function supports type02, type03 client/server style inter-process communication.

Parameters:

Receive Message

Normally, you don’t have to call receive functions. They are used in type03 inter-process communication context.

berrymq.get(token[, block[, timeout]])
berrymq.get_nowait(token)

Inter-process settings

berrymq.init_connection(addr=("localhost", port=0))

If you want to use style01 inter-process communication feature, call this function first.

New in version 0.2.

berrymq.interconnect(addr[, ttl=1000])

Connect to other node in style01.

Parameter:addr (tuple) – tuple which contains hostname and port number

New in version 0.2.

berrymq.connect_oneway(addr[, ttl=1000])

Connect to other node in style02.

Parameter:addr (tuple) – tuple which contains hostname and port number

New in version 0.2.

berrymq.connect_via_queue(addr, identfier[, ttl=1000])

Connect to other node in style03. Identifier is a filter of queue input.

Parameters:
  • addr (tuple) – tuple which contains hostname and port number
  • identifier (str) – filter of queue

New in version 0.2.

berrymq.send_message(identifier[, ...])

Send message to other nodes. If you use interconnect() to connect, you don’t have to this method(forward twitter automatically).

New in version 0.2.

berrymq.get([block=True[, timeout=1000]])
Returns:message
Return type:Message

New in version 0.2.

berrymq.get_nowait()
Returns:message
Return type:Message

New in version 0.2.

berrymq.close_connection([addr])

Close connection.

Parameter:addr (tuple) – tuple which contains hostname and port number

New in version 0.2.

Concurrency

berrymq.set_multiplicity(number)
This function is set thread pool size.

Support Functions

berrymq.regist_function(identifier, function)
berrymq.regist_method(identifier, method)
These functions is used for changing identifier dynamically.

Classes

class berrymq.Follower

If you want to use the method as receiver, set this class as metaclass.

In python 2.4 - 2.6:

# Python 2.4, 2.5, 2.6
class Logger(object):
    __metaclass__ = Follower
    @following("*:log")
    def receive_log(self, message):
            ...

In python 3.0 -:

# Python 3.0, 3.1
class Logger(metaclass=Follower):
    @following("*:log")
    def receive_log(self, message):
       ...

If you use Ruby, This class provide special decorators. following() and auto_twitter(). Use like this:

# Ruby
class Logger
  include BerryMQ::Follower

  following("*:log")
  def receive_log(message)
     ...
  end
end
class berrymq.Message

This object is created in berryMQ automatically. User doesn’t create this object directly.

All of following attributes are readonly(defined as property).

name
This is a front part of Identifier.
action
This is a back part of Identifier.
id
This is a string form of Identifier
args

If you pass any parameters at twitter(), this attribute stores them.

.
twitter("do_something:log", time.ctime())
.

@following("*:log)
def receive_log(message):
    print(message.args[0])  # print time.ctime() value
kwargs

It is similar to args. If you pass keyword argument, you can access that value via this property.

See also

args

class berrymq.Queue

This is a key class of pull API for message receiving.

queue = berrymq.Queue("task:*")

# wait until someone send "task:*" message
message = queue.get()

Method name of this class is similar to Python’s standard library. This class doesn’t have put() method, because of all stored items are sent by berryMQ. User uses this object just as message receiver.

__init__(identifier[, shared=False])

Create new method queue.

If you pass True at shared flag, new object become a shared queue. If there are some objects which has same Identifier, they share only one queue. If some object get() value, others can’t get that message any more.

In another case, new object become a standalone queue. If there are some objects which has same Identifier and someone send message which matches that identifier, all queues will store a copy of that message.

Parameter:identifier – works as a filter. see Identifier.
empty()

Return True if the queue object is empty.

If you use this class in concurrency envirionment, this result is not reliable because other thread put new value after calling this method.

full()
Return True if the queue object is full. This return value is not reliable because of same reason of empty().
qsize()
Return stored item number. This return value is not reliable because of same reason of empty().
get([block=True[, timeout=None]])

Return stored message. This is a FIFO queue.

If block is True (defalut), function call is blocked while new message received. If False it returns immediately even if the queue is empty.

Parameters:
  • block (True of False) – Blocking mode flag.
  • timeout – Set timeout[sec]. It is used in blocking mode.
Returns:

stored Message object

Note

Future release supports priority queue.

get_nowait()
This method is alias of get(block=False).
exception berrymq.Empty
This is a same class of queue.Empty. Its object is raised by Queue.get()

Adapters

New in version 0.2.

Growl Adapter

class berrymq.adapter.growl.GrowlAdapter

This is a adapter class for Growl. You can forward message to Growl. This class uses UDP protocol of Growl.

Note

Future release will support Growl Notification Transfer Protocol(GNTP). It supports call back mechanism.

__init__(identifier)
format(message)
This is template method fou formatting message. Override this method. This method should return string.

New in version 0.3.

File Observer

class berrymq.adapter.fileobserver.FileObserver

This is a adapter class for observing file system. This adapter can notify following change:

  • created
  • modified
  • removed

sample:

observer = berrymq.adapter.fileobserver.FileObserver("~/log/*.log", "local_log")

If new log file is created, this object twitters local_log:created message automatically. Anyone modify log files, local_log:modified will be sent. Of course, local_log:removed message will be sent if any file is removed.

__init__(target_dir, id_name[, interval=5])
Parameters:
  • target_dir (str) – Checking condition (see glob’s document).
  • id_name (str) – This value is used for message id
  • interval (int) – File system checking inteval
stop()
This method stop observing.

New in version 0.3.

Timer

class berrymq.adapter.timer.IntervalTimer

This is a simple intarval timer. Following adapter sends timer:tick message every 10 seconds.

timer = berrymq.adapter.timer.IntervalTimer("timer", 10)
__init__(id_name, interval)
Parameters:
  • id_name (str) – This value is used for message id
  • interval (int) – This adapter send
stop()
This method stop observing.

New in version 0.3.

wxPython

class berrymq.adapter.wxpython.wxPythonAdapter

This class convert berryMQ message into wxPython’s event.

__init__(window, id_filter)
Parameters:
  • window (wx.Window) – This is a wxPython’s window that handles events.
  • id_filter (str) – The message will transfer to wxPython if matches it
class berrymq.adapter.wxpython.BerryMQEvent
This is event class of wxPython.
berrymq.adapter.wxpython.EVT_BERRYMQ_MSG()
This is an event binder. See wxPython’s document.

Table Of Contents

Previous topic

Introduction

Next topic

Ruby API

This Page