@@ -1757,17 +1757,25 @@ async def prep(event: EventBase) -> None:
17571757
17581758 events_and_contexts_to_persist .append ((event , context ))
17591759
1760- for event in sorted_auth_events :
1760+ for i , event in enumerate ( sorted_auth_events ) :
17611761 await prep (event )
17621762
1763- await self .persist_events_and_notify (
1764- room_id ,
1765- events_and_contexts_to_persist ,
1766- # Mark these events backfilled as they're historic events that will
1767- # eventually be backfilled. For example, missing events we fetch
1768- # during backfill should be marked as backfilled as well.
1769- backfilled = True ,
1770- )
1763+ # The above function is typically not async, and so won't yield to
1764+ # the reactor. For large rooms let's yield to the reactor
1765+ # occasionally to ensure we don't block other work.
1766+ if (i + 1 ) % 1000 == 0 :
1767+ await self ._clock .sleep (0 )
1768+
1769+ # Also persist the new event in batches for similar reasons as above.
1770+ for batch in batch_iter (events_and_contexts_to_persist , 1000 ):
1771+ await self .persist_events_and_notify (
1772+ room_id ,
1773+ batch ,
1774+ # Mark these events as backfilled as they're historic events that will
1775+ # eventually be backfilled. For example, missing events we fetch
1776+ # during backfill should be marked as backfilled as well.
1777+ backfilled = True ,
1778+ )
17711779
17721780 @trace
17731781 async def _check_event_auth (
0 commit comments