Skip to content

Mules

from uwsgiconf.runtime.mules import Mule, Farm

first_mule = Mule(1)

@first_mule.offload()
def for_mule(*args, **kwargs):
    # This function will be offloaded to and handled by mule 1.
    ...

farm_two = Farm('two')

@farm_two.offload()
def for_farm(*args, **kwargs):
    # And this one will be offloaded to farm `two` and handled by any mule from that farm.
    ...

Functions

mule_offload

1
2
3
mule_offload(
    mule_or_farm: TypeMuleFarm = None,  # (1)!
) -> Callable
  1. If not set, offloads to a first mule.

Decorator. Use to offload function execution to a mule or a farm.


Classes

Farm

Farm(name: str, mules: list[int] | None = None)

Represents uWSGI Mule Farm.

Note

Register farms before using this. E.g.: section.workers.set_mules_params(farms=section.workers.mule_farm('myfarm', 2))


is_mine

.is_mine()

Returns flag indicating whether the current mule belongs to this farm.


get_farms

.get_farms() -> list[Farm]

Returns a list of registered farm objects.

1
2
3
farms = Farm.get_farms()
first_farm = farms[0]
first_farm_first_mule = first_farm.mules[0]

get_message

.get_message() -> str

Reads a mule farm message.

ValueError If not in a mule


offload

.offload() -> Callable

Decorator. Allows to offload function execution on mules of this farm.

1
2
3
4
5
6
first_mule = Farm('myfarm')

@first_mule.offload()
def for_mule(*args, **kwargs):
    # This function will be offloaded to farm `myfarm` and handled by any mule from that farm.
    ...

send

.send(message: str | bytes)

Sends a message to the given farm.


Mule

Mule(id: int)

Represents uWSGI Mule.

Note

Register mules before using this. E.g.: section.workers.set_mules_params(mules=3)


get_current

.get_current() -> Optional[Mule]

Returns current mule object or None if not a mule.


get_current_id

.get_current_id() -> int

Returns current mule ID. Returns 0 if not a mule.


get_message

1
2
3
4
5
6
.get_message(
    signals: bool = True,  # (1)!
    farms: bool = False,  # (2)!
    buffer_size: int = 65536, 
    timeout: int = -1,  # (3)!
) -> str
  1. Whether to manage signals.

  2. Whether to manage farms.

  3. Seconds.

Block until a mule message is received and return it.

This can be called from multiple threads in the same programmed mule.

ValueError If not in a mule.


offload

.offload() -> Callable

Decorator. Allows to offload function execution on this mule.

1
2
3
4
5
6
first_mule = Mule(1)

@first_mule.offload()
def for_mule(*args, **kwargs):
    # This function will be offloaded to and handled by mule 1.
    ...

send

.send(message: str | bytes) -> bool

Sends a message to a mule(s)/farm.

ValueError If no mules, or mule ID or farm name is not recognized.