@@ -1917,6 +1917,23 @@ channel_is_associated(_channels *channels, int64_t cid, int64_t interpid,
1917
1917
return (end != NULL && end -> open );
1918
1918
}
1919
1919
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
+
1920
1937
1921
1938
/* channel info */
1922
1939
@@ -3215,6 +3232,34 @@ Close the channel for the current interpreter. 'send' and 'recv'\n\
3215
3232
(bool) may be used to indicate the ends to close. By default both\n\
3216
3233
ends are closed. Closing an already closed end is a noop." );
3217
3234
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
+
3218
3263
static PyObject *
3219
3264
channelsmod_get_info (PyObject * self , PyObject * args , PyObject * kwds )
3220
3265
{
@@ -3341,6 +3386,8 @@ static PyMethodDef module_functions[] = {
3341
3386
METH_VARARGS | METH_KEYWORDS , channelsmod_close_doc },
3342
3387
{"release" , _PyCFunction_CAST (channelsmod_release ),
3343
3388
METH_VARARGS | METH_KEYWORDS , channelsmod_release_doc },
3389
+ {"get_count" , _PyCFunction_CAST (channelsmod_get_count ),
3390
+ METH_VARARGS | METH_KEYWORDS , channelsmod_get_count_doc },
3344
3391
{"get_info" , _PyCFunction_CAST (channelsmod_get_info ),
3345
3392
METH_VARARGS | METH_KEYWORDS , channelsmod_get_info_doc },
3346
3393
{"get_channel_defaults" , _PyCFunction_CAST (channelsmod_get_channel_defaults ),
0 commit comments