Rebroadcasting data¶
nebra.rebroadcast(target, args=(), kwargs=None, max_retries=7, initial_retry_delay=1.0, send_function=send, **send_kwargs)
¶
Rebroadcast events from a producer function onto the AT Protocol. Includes threading for speed, robust error handling that automatically reboots clients that crash, a Queue system to deal with busy networks or rate limited PDSs, and more!
This function is the recommended way to rebroadcast events onto the AT Protocol from an existing source in a robust way.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
Callable
|
A function that produces events and adds them to the data source. This function should take a DataSource as its first argument, followed by any additional arguments specified in args and kwargs. |
required |
args
|
tuple
|
Positional arguments to pass to the producer function. Defaults to (). |
()
|
kwargs
|
dict
|
Keyword arguments to pass to the producer function. Defaults to None. |
None
|
max_retries
|
int
|
Maximum number of retry attempts for failed sends. Defaults to 7. |
7
|
initial_retry_delay
|
float
|
Initial delay in seconds for retry attempts (exponential backoff). Defaults to 1.0. |
1.0
|
send_function
|
Callable
|
Function to use for sending events. Defaults to nebra.client.send. |
send
|
**send_kwargs
|
dict
|
Additional keyword arguments to pass to the send function. |
{}
|
Source code in nebra/broadcast.py
nebra.DataSource
¶
Class for managing event sources with thread-safe queue operations.
This class provides a thread-safe queue for communication between event producers and consumers, along with methods for monitoring the producer's activity.
Attributes:
| Name | Type | Description |
|---|---|---|
stop_event |
Event
|
An event to signal when the data source should stop. |
Methods:
| Name | Description |
|---|---|
add_event |
Add an event to the queue. |
get_event |
Get an event from the queue, with optional timeout. |
is_active |
Check if the data source is active (not stopped). |
mark_producer_running |
Mark the producer as running by recording the current time. This method is thread-safe. |
producer_last_signal |
Get the number of seconds since mark_producer_running() was last called. |
Examples:
The following example shows how to use a DataSource with a callable:
import time
import nebra
def post_producer(data_source: nebra.DataSource):
counter = 1
while data_source.is_active():
# Mark that the producer is running
data_source.mark_producer_running()
new_post = {
"$type": "app.bsky.feed.post",
"text": f"This is test post {counter}.",
"createdAt": nebra.get_atproto_utc_time()
}
data_source.add_event(new_post)
counter += 1
time.sleep(1)
# Producer has been stopped
print(f"Producer last signal was {data_source.producer_last_signal():.2f} seconds ago")
class PostConsumer:
def __init__(self, data_source: nebra.DataSource):
self.data_source = data_source
def process_events(self):
while self.data_source.is_active():
try:
# Get an event with a 1-second timeout
event = self.data_source.get_event(timeout=1.0)
print(f"Processing event: {event}")
except queue.Empty:
# No event available, continue checking if active
continue
Source code in nebra/broadcast.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | |
__init__(max_queue_size=1000)
¶
Initialize the DataSource with an event queue.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_queue_size
|
int
|
Maximum size of the event queue. Defaults to 1000. |
1000
|
Source code in nebra/broadcast.py
add_event(event)
¶
Add an event to the queue.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
dict[str, Any]
|
The event to add to the queue. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the event was added, False if the queue was full. |
Source code in nebra/broadcast.py
get_event(timeout=None)
¶
Get an event from the queue.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timeout
|
float | None
|
Maximum time to wait for an event, in seconds. If None, waits indefinitely. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The event from the queue. |
Raises:
| Type | Description |
|---|---|
Empty
|
If no event is available within the specified timeout. |
Source code in nebra/broadcast.py
is_active()
¶
Check if the data source is active (not stopped).
Returns:
| Type | Description |
|---|---|
bool
|
True if the data source is active (stop_event is not set), False otherwise. |
mark_producer_running()
¶
Mark the producer as running by recording the current time.
This method is thread-safe and can be called from any thread. The time since this was last called can be accessed with producer_last_signal().
Source code in nebra/broadcast.py
producer_last_signal()
¶
Get the number of seconds since mark_producer_running() was last called.
Returns:
| Type | Description |
|---|---|
float
|
The number of seconds since mark_producer_running() was last called. If mark_producer_running() has never been called, returns the time since the DataSource was initialized. |
Source code in nebra/broadcast.py
stop()
¶
Signal the data source to stop.
This method sets the stop_event, which should be checked periodically in the producer function to allow for clean shutdown.