> ## Documentation Index
> Fetch the complete documentation index at: https://abxbus.archivebox.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Async and Sync Handlers

> Mix sync and async handlers across functions and method styles.

Python, TypeScript, and Rust support registering sync and async handlers together. Go handlers use one context-aware function shape; handlers can return immediately, block, or coordinate goroutines, while AbxBus controls event/handler concurrency with bus options.

Supported handler shapes:

* bare functions (sync or async)
* static methods (sync or async)
* class-level methods (Python `@classmethod`; in TypeScript, class-level handlers are `static` methods)
* instance methods (sync or async)

<Tabs>
  <Tab title="Python">
    ```python theme={null}
    import asyncio
    from abxbus import EventBus, BaseEvent

    class WorkEvent(BaseEvent[str]):
        task_id: str

    def bare_sync(event: WorkEvent) -> str:
        return f'bare-sync:{event.task_id}'

    async def bare_async(event: WorkEvent) -> str:
        await asyncio.sleep(0.01)
        return f'bare-async:{event.task_id}'

    class HandlerSet:
        def __init__(self, prefix: str) -> None:
            self.prefix = prefix

        @staticmethod
        def static_sync(event: WorkEvent) -> str:
            return f'static-sync:{event.task_id}'

        @staticmethod
        async def static_async(event: WorkEvent) -> str:
            await asyncio.sleep(0.01)
            return f'static-async:{event.task_id}'

        @classmethod
        def class_sync(cls, event: WorkEvent) -> str:
            return f'{cls.__name__}-class-sync:{event.task_id}'

        @classmethod
        async def class_async(cls, event: WorkEvent) -> str:
            await asyncio.sleep(0.01)
            return f'{cls.__name__}-class-async:{event.task_id}'

        def instance_sync(self, event: WorkEvent) -> str:
            return f'{self.prefix}-instance-sync:{event.task_id}'

        async def instance_async(self, event: WorkEvent) -> str:
            await asyncio.sleep(0.01)
            return f'{self.prefix}-instance-async:{event.task_id}'

    bus = EventBus('AppBus')
    handlers = HandlerSet(prefix='svc')

    bus.on(WorkEvent, bare_sync)
    bus.on(WorkEvent, bare_async)
    bus.on(WorkEvent, HandlerSet.static_sync)
    bus.on(WorkEvent, HandlerSet.static_async)
    bus.on(WorkEvent, HandlerSet.class_sync)
    bus.on(WorkEvent, HandlerSet.class_async)
    bus.on(WorkEvent, handlers.instance_sync)
    bus.on(WorkEvent, handlers.instance_async)
    ```
  </Tab>

  <Tab title="TypeScript">
    ```ts theme={null}
    import { BaseEvent, EventBus } from 'abxbus'
    import { z } from 'zod'

    const WorkEvent = BaseEvent.extend('WorkEvent', {
      task_id: z.string(),
    })

    const bus = new EventBus('AppBus')

    const bareSync = (event: InstanceType<typeof WorkEvent>) => `bare-sync:${event.task_id}`
    const bareAsync = async (event: InstanceType<typeof WorkEvent>) => {
      await new Promise((resolve) => setTimeout(resolve, 10))
      return `bare-async:${event.task_id}`
    }

    class HandlerSet {
      constructor(private prefix: string) {}

      static staticSync(event: InstanceType<typeof WorkEvent>) {
        return `static-sync:${event.task_id}`
      }

      static async staticAsync(event: InstanceType<typeof WorkEvent>) {
        await new Promise((resolve) => setTimeout(resolve, 10))
        return `static-async:${event.task_id}`
      }

      instanceSync(event: InstanceType<typeof WorkEvent>) {
        return `${this.prefix}-instance-sync:${event.task_id}`
      }

      async instanceAsync(event: InstanceType<typeof WorkEvent>) {
        await new Promise((resolve) => setTimeout(resolve, 10))
        return `${this.prefix}-instance-async:${event.task_id}`
      }
    }

    const handlers = new HandlerSet('svc')

    bus.on(WorkEvent, bareSync)
    bus.on(WorkEvent, bareAsync)
    bus.on(WorkEvent, HandlerSet.staticSync)
    bus.on(WorkEvent, HandlerSet.staticAsync)
    bus.on(WorkEvent, handlers.instanceSync.bind(handlers))
    bus.on(WorkEvent, handlers.instanceAsync.bind(handlers))
    ```
  </Tab>

  <Tab title="Rust">
    ```rust theme={null}
    use abxbus::{
        event,
        event_bus::EventBus,
        BaseEvent,
    };
    use futures_timer::Delay;
    use std::time::Duration;

    event! {
        struct WorkEvent {
            task_id: String,
            event_result_type: String,
        }
    }

    let bus = EventBus::new(Some("AppBus".to_string()));

    bus.on(WorkEvent, |event: WorkEvent| async move {
        Ok(format!("bare-sync:{}", event.task_id))
    });

    bus.on(WorkEvent, |event: WorkEvent| async move {
        Delay::new(Duration::from_millis(10)).await;
        Ok(format!("bare-async:{}", event.task_id))
    });

    struct HandlerSet {
        prefix: String,
    }

    let handlers = HandlerSet {
        prefix: "svc".to_string(),
    };
    bus.on(WorkEvent, move |event: WorkEvent| async move {
        Ok(format!("{}-instance:{}", handlers.prefix, event.task_id))
    });
    ```
  </Tab>

  <Tab title="Go">
    ```go theme={null}
    package main

    import (
    	"fmt"
    	"time"

    	abxbus "github.com/ArchiveBox/abxbus/abxbus-go"
    )

    func bareHandler(event *abxbus.BaseEvent) (any, error) {
    	return fmt.Sprintf("bare:%s", event.Payload["task_id"]), nil
    }

    type HandlerSet struct {
    	Prefix string
    }

    func StaticStyleHandler(event *abxbus.BaseEvent) (any, error) {
    	time.Sleep(10 * time.Millisecond)
    	return fmt.Sprintf("static:%s", event.Payload["task_id"]), nil
    }

    func (h *HandlerSet) InstanceHandler(event *abxbus.BaseEvent) (any, error) {
    	return fmt.Sprintf("%s-instance:%s", h.Prefix, event.Payload["task_id"]), nil
    }

    func main() {
    	bus := abxbus.NewEventBus("AppBus", nil)
    	handlers := &HandlerSet{Prefix: "svc"}

    	bus.On("WorkEvent", "bare_handler", bareHandler, nil)
    	bus.On("WorkEvent", "static_style_handler", StaticStyleHandler, nil)
    	bus.On("WorkEvent", "instance_handler", handlers.InstanceHandler, nil)
    }
    ```
  </Tab>
</Tabs>
