3
3
import os
4
4
import threading
5
5
import asyncio
6
- import inspect
7
6
8
7
from time import sleep , time
9
8
from sentry_sdk ._queue import Queue , FullError
@@ -192,7 +191,7 @@ def _target(self) -> None:
192
191
193
192
class AsyncWorker (Worker ):
194
193
def __init__ (self , queue_size : int = DEFAULT_QUEUE_SIZE ) -> None :
195
- self ._queue : asyncio .Queue [Any ] = asyncio . Queue ( queue_size )
194
+ self ._queue : asyncio .Queue [Any ] = None
196
195
self ._task : Optional [asyncio .Task [None ]] = None
197
196
# Event loop needs to remain in the same process
198
197
self ._task_for_pid : Optional [int ] = None
@@ -228,10 +227,13 @@ def start(self) -> None:
228
227
if not self .is_alive :
229
228
try :
230
229
self ._loop = asyncio .get_running_loop ()
230
+ if self ._queue is None :
231
+ self ._queue = asyncio .Queue (maxsize = self ._queue_size )
231
232
self ._task = self ._loop .create_task (self ._target ())
232
233
self ._task_for_pid = os .getpid ()
233
234
except RuntimeError :
234
235
# There is no event loop running
236
+ logger .warning ("No event loop running, async worker not started" )
235
237
self ._loop = None
236
238
self ._task = None
237
239
self ._task_for_pid = None
@@ -253,7 +255,7 @@ async def _wait_flush(self, timeout: float, callback: Optional[Any] = None) -> N
253
255
try :
254
256
await asyncio .wait_for (self ._queue .join (), timeout = initial_timeout )
255
257
except asyncio .TimeoutError :
256
- pending = self ._queue .qsize () + 1
258
+ pending = self ._queue .qsize () + len ( self . _active_tasks )
257
259
logger .debug ("%d event(s) pending on flush" , pending )
258
260
if callback is not None :
259
261
callback (pending , timeout )
@@ -262,7 +264,7 @@ async def _wait_flush(self, timeout: float, callback: Optional[Any] = None) -> N
262
264
remaining_timeout = timeout - initial_timeout
263
265
await asyncio .wait_for (self ._queue .join (), timeout = remaining_timeout )
264
266
except asyncio .TimeoutError :
265
- pending = self ._queue .qsize () + 1
267
+ pending = self ._queue .qsize () + len ( self . _active_tasks )
266
268
logger .error ("flush timed out, dropped %s events" , pending )
267
269
268
270
async def flush_async (self , timeout : float , callback : Optional [Any ] = None ) -> None :
@@ -296,12 +298,8 @@ async def _target(self) -> None:
296
298
await asyncio .sleep (0 )
297
299
298
300
async def _process_callback (self , callback : Callable [[], Any ]) -> None :
299
- if inspect .iscoroutinefunction (callback ):
300
- # Callback is an async coroutine, need to await it
301
- await callback ()
302
- else :
303
- # Callback is a sync function, need to call it
304
- callback ()
301
+ # Callback is an async coroutine, need to await it
302
+ await callback ()
305
303
306
304
def _on_task_complete (self , task : asyncio .Task [None ]) -> None :
307
305
try :
0 commit comments