@@ -1917,6 +1917,23 @@ channel_is_associated(_channels *channels, int64_t cid, int64_t interpid,
19171917 return (end != NULL && end -> open );
19181918}
19191919
1920+ static int
1921+ _channel_get_count (_channels * channels , int64_t cid , Py_ssize_t * p_count )
1922+ {
1923+ PyThread_type_lock mutex = NULL ;
1924+ _channel_state * chan = NULL ;
1925+ int err = _channels_lookup (channels , cid , & mutex , & chan );
1926+ if (err != 0 ) {
1927+ return err ;
1928+ }
1929+ assert (chan != NULL );
1930+ int64_t count = chan -> queue -> count ;
1931+ PyThread_release_lock (mutex );
1932+
1933+ * p_count = (Py_ssize_t )count ;
1934+ return 0 ;
1935+ }
1936+
19201937
19211938/* channel info */
19221939
@@ -3215,6 +3232,34 @@ Close the channel for the current interpreter. 'send' and 'recv'\n\
32153232(bool) may be used to indicate the ends to close. By default both\n\
32163233ends are closed. Closing an already closed end is a noop." );
32173234
3235+ static PyObject *
3236+ channelsmod_get_count (PyObject * self , PyObject * args , PyObject * kwds )
3237+ {
3238+ static char * kwlist [] = {"cid" , NULL };
3239+ struct channel_id_converter_data cid_data = {
3240+ .module = self ,
3241+ };
3242+ if (!PyArg_ParseTupleAndKeywords (args , kwds ,
3243+ "O&:get_count" , kwlist ,
3244+ channel_id_converter , & cid_data )) {
3245+ return NULL ;
3246+ }
3247+ int64_t cid = cid_data .cid ;
3248+
3249+ Py_ssize_t count = -1 ;
3250+ int err = _channel_get_count (& _globals .channels , cid , & count );
3251+ if (handle_channel_error (err , self , cid )) {
3252+ return NULL ;
3253+ }
3254+ assert (count >= 0 );
3255+ return PyLong_FromSsize_t (count );
3256+ }
3257+
3258+ PyDoc_STRVAR (channelsmod_get_count_doc ,
3259+ "get_count(cid)\n\
3260+ \n\
3261+ Return the number of items in the channel." );
3262+
32183263static PyObject *
32193264channelsmod_get_info (PyObject * self , PyObject * args , PyObject * kwds )
32203265{
@@ -3341,6 +3386,8 @@ static PyMethodDef module_functions[] = {
33413386 METH_VARARGS | METH_KEYWORDS , channelsmod_close_doc },
33423387 {"release" , _PyCFunction_CAST (channelsmod_release ),
33433388 METH_VARARGS | METH_KEYWORDS , channelsmod_release_doc },
3389+ {"get_count" , _PyCFunction_CAST (channelsmod_get_count ),
3390+ METH_VARARGS | METH_KEYWORDS , channelsmod_get_count_doc },
33443391 {"get_info" , _PyCFunction_CAST (channelsmod_get_info ),
33453392 METH_VARARGS | METH_KEYWORDS , channelsmod_get_info_doc },
33463393 {"get_channel_defaults" , _PyCFunction_CAST (channelsmod_get_channel_defaults ),
0 commit comments