Skip to main content

Python asynchronous I/O package for LDMud

Project description

Python asynchronous I/O package for LDMud

This is a python module that enables the use of the Python asyncio module with LDMud 3.6.2 and later. The module doesn't export any names, it just extends the Python asyncio module to work with LDMud.


Install from the python package index

The efun package can be downloaded from the python package index:

pip3 install --user ldmud-asyncio

Build & install the package yourself

You can build the package yourself.

First clone the repository

git clone

Install the package

cd python-asyncio
python3 install --user

Automatically load the module at startup

In your Python startup script for LDMud add the following line:

import ldmud_asyncio


call_out() replacement

import ldmud, ldmud_asyncio, asyncio

async def do_call_out(cb, sec):
    await asyncio.sleep(sec)

def efun_call_out(cb: ldmud.Closure, sec: int) -> None:, sec))

ldmud.register_efun("py_call_out", efun_call_out)

Call a program and return the lines

import ldmud, ldmud_asyncio, asyncio

async def do_exec(prog, cb):
    proc = await asyncio.create_subprocess_exec(prog, stdout=asyncio.subprocess.PIPE)

    async for line in proc.stdout:

    await proc.wait()

def efun_exec(prog: str, cb: ldmud.Closure) -> None:, cb))

ldmud.register_efun("py_exec", efun_exec)

Start a websockets server

This is the synchronization example from the websockets package. Here we only added an efun to influence the value as well.

This is the Python code:

import ldmud, ldmud_asyncio, asyncio, websockets, json

STATE = {"value": 0}
USERS = set()

def state_event():
    return json.dumps({"type": "state", **STATE})

def users_event():
    return json.dumps({"type": "users", "count": len(USERS)})

async def notify_state():
    if USERS:  # asyncio.wait doesn't accept an empty list
        message = state_event()
        await asyncio.wait([user.send(message) for user in USERS])

async def notify_users():
    if USERS:  # asyncio.wait doesn't accept an empty list
        message = users_event()
        await asyncio.wait([user.send(message) for user in USERS])

async def register(websocket):
    await notify_users()

async def unregister(websocket):
    await notify_users()

async def counter(websocket, path):
    # register(websocket) sends user_event() to websocket
    await register(websocket)
        await websocket.send(state_event())
        async for message in websocket:
            data = json.loads(message)
            if data["action"] == "minus":
                STATE["value"] -= 1
                await notify_state()
            elif data["action"] == "plus":
                STATE["value"] += 1
                await notify_state()
        await unregister(websocket), "localhost", 6789))

async def do_ws_set_value(val):
    STATE["value"] = val
    await notify_state()

def efun_ws_set_value(val: int) -> None:

ldmud.register_efun("py_ws_set_value", efun_ws_set_value)

And here the HTML code to run in the browser:

<!DOCTYPE html>
        <title>WebSocket demo</title>
        <style type="text/css">
            body {
                font-family: "Courier New", sans-serif;
                text-align: center;
            .buttons {
                font-size: 4em;
                display: flex;
                justify-content: center;
            .button, .value {
                line-height: 1;
                padding: 2rem;
                margin: 2rem;
                border: medium solid;
                min-height: 1em;
                min-width: 1em;
            .button {
                cursor: pointer;
                user-select: none;
            .minus {
                color: red;
            .plus {
                color: green;
            .value {
                min-width: 2em;
            .state {
                font-size: 2em;
        <div class="buttons">
            <div class="minus button">-</div>
            <div class="value">?</div>
            <div class="plus button">+</div>
        <div class="state">
            <span class="users">?</span> online
            var minus = document.querySelector('.minus'),
                plus = document.querySelector('.plus'),
                value = document.querySelector('.value'),
                users = document.querySelector('.users'),
                websocket = new WebSocket("ws://");
            minus.onclick = function (event) {
                websocket.send(JSON.stringify({action: 'minus'}));
            plus.onclick = function (event) {
                websocket.send(JSON.stringify({action: 'plus'}));
            websocket.onmessage = function (event) {
                data = JSON.parse(;
                switch (data.type) {
                    case 'state':
                        value.textContent = data.value;
                    case 'users':
                        users.textContent = (
                            data.count.toString() + " user" +
                            (data.count == 1 ? "" : "s"));
                            "unsupported event", data);

Have fun!

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for ldmud-asyncio, version 0.0.2
Filename, size File type Python version Upload date Hashes
Filename, size ldmud_asyncio-0.0.2-py3-none-any.whl (8.9 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size ldmud-asyncio-0.0.2.tar.gz (6.0 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page