@@ -408,12 +408,31 @@ static PyObject *Producer_poll (Handle *self, PyObject *args,
408
408
}
409
409
410
410
411
- static PyObject * Producer_flush (Handle * self , PyObject * ignore ) {
412
- while (rd_kafka_outq_len (self -> rk ) > 0 ) {
413
- if (Producer_poll0 (self , 500 ) == -1 )
414
- return NULL ;
415
- }
416
- Py_RETURN_NONE ;
411
+ static PyObject * Producer_flush (Handle * self , PyObject * args ,
412
+ PyObject * kwargs ) {
413
+ double tmout = -1 ;
414
+ int qlen ;
415
+ static char * kws [] = { "timeout" , NULL };
416
+ #if RD_KAFKA_VERSION >= 0x00090300
417
+ CallState cs ;
418
+ #endif
419
+
420
+ if (!PyArg_ParseTupleAndKeywords (args , kwargs , "|d" , kws , & tmout ))
421
+ return NULL ;
422
+
423
+ #if RD_KAFKA_VERSION >= 0x00090300
424
+ CallState_begin (self , & cs );
425
+ rd_kafka_flush (self -> rk , tmout < 0 ? -1 : (int )(tmout * 1000 ));
426
+ if (!CallState_end (self , & cs ))
427
+ return NULL ;
428
+ qlen = rd_kafka_outq_len (self -> rk );
429
+ #else
430
+ while ((qlen = rd_kafka_outq_len (self -> rk )) > 0 ) {
431
+ if (Producer_poll0 (self , 500 ) == -1 )
432
+ return NULL ;
433
+ }
434
+ #endif
435
+ return PyLong_FromLong (qlen );
417
436
}
418
437
419
438
@@ -463,11 +482,16 @@ static PyMethodDef Producer_methods[] = {
463
482
"\n"
464
483
},
465
484
466
- { "flush" , (PyCFunction )Producer_flush , METH_NOARGS ,
485
+ { "flush" , (PyCFunction )Producer_flush , METH_VARARGS |METH_KEYWORDS ,
486
+ ".. py:function:: flush([timeout])\n"
487
+ "\n"
467
488
" Wait for all messages in the Producer queue to be delivered.\n"
468
489
" This is a convenience method that calls :py:func:`poll()` until "
469
- ":py:func:`len()` is zero.\n"
490
+ ":py:func:`len()` is zero or the optional timeout elapses .\n"
470
491
"\n"
492
+ " :param: float timeout: Maximum time to block (requires librdkafka >= v0.9.3).\n"
493
+ " :returns: Number of messages still in queue.\n"
494
+ "\n"
471
495
".. note:: See :py:func:`poll()` for a description on what "
472
496
"callbacks may be triggered.\n"
473
497
"\n"
0 commit comments