Skip to content

bpo-40390: Implement channel_send_wait for subinterpreters #19715

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Implement channel_send_wait
  • Loading branch information
Benjamin Edwards committed Apr 19, 2020
commit d4549413221399b4674b10b12a0818d4777a8c20
31 changes: 31 additions & 0 deletions Lib/test/test__xxsubinterpreters.py
Original file line number Diff line number Diff line change
Expand Up @@ -1337,6 +1337,37 @@ def test_run_string_arg_resolved(self):
self.assertEqual(obj, b'spam')
self.assertEqual(out.strip(), 'send')

def test_send_wait_revc(self):
cid = interpreters.channel_create()
interp = interpreters.create()
def run():
_run_output(interp, dedent(f"""
import _xxsubinterpreters as _interpreters
import time
_interpreters.channel_send_wait({cid}, b"send")
#assert False
#while True:
# try:
# obj = _interpreters.channel_recv({cid})
# break
# except _interpreters.ChannelEmptyError:
# time.sleep(0.1)
"""))
t = threading.Thread(target=run)
#import pdb; pdb.set_trace()
t.start()
#interpreters.channel_send_wait(cid, b"send", timeout=1)
#import pdb; pdb.set_trace()
while True:
try:
obj = interpreters.channel_recv(cid)
#import pdb; pdb.set_trace()
assert obj == b"send"
break
except interpreters.ChannelEmptyError:
time.sleep(1)
t.join()

# close

def test_close_single_user(self):
Expand Down
77 changes: 71 additions & 6 deletions Modules/_xxsubinterpretersmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,8 @@ struct _channelitem;

typedef struct _channelitem {
_PyCrossInterpreterData *data;
/* The lock is owned by the sender. */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a helpful comment. 😄

PyThread_type_lock lock;
struct _channelitem *next;
} _channelitem;

Expand Down Expand Up @@ -393,6 +395,9 @@ _channelitem_free_all(_channelitem *item)
while (item != NULL) {
_channelitem *last = item;
item = item->next;
if (last->lock != NULL) {
PyThread_release_lock(last->lock);
}
_channelitem_free(last);
}
}
Expand All @@ -402,6 +407,9 @@ _channelitem_popped(_channelitem *item)
{
_PyCrossInterpreterData *data = item->data;
item->data = NULL;
if (item->lock != NULL) {
PyThread_release_lock(item->lock);
}
_channelitem_free(item);
return data;
}
Expand Down Expand Up @@ -443,13 +451,18 @@ _channelqueue_free(_channelqueue *queue)
}

static int
_channelqueue_put(_channelqueue *queue, _PyCrossInterpreterData *data)
_channelqueue_put(_channelqueue *queue, _PyCrossInterpreterData *data,
PyThread_type_lock lock)
{
_channelitem *item = _channelitem_new();
if (item == NULL) {
return -1;
}
item->data = data;
item->lock = lock;
if (lock != NULL) {
PyThread_acquire_lock(item->lock, WAIT_LOCK);
}

queue->count += 1;
if (queue->first == NULL) {
Expand Down Expand Up @@ -761,7 +774,7 @@ _channel_free(_PyChannelState *chan)

static int
_channel_add(_PyChannelState *chan, int64_t interp,
_PyCrossInterpreterData *data)
_PyCrossInterpreterData *data, PyThread_type_lock lock)
{
int res = -1;
PyThread_acquire_lock(chan->mutex, WAIT_LOCK);
Expand All @@ -774,7 +787,7 @@ _channel_add(_PyChannelState *chan, int64_t interp,
goto done;
}

if (_channelqueue_put(chan->queue, data) != 0) {
if (_channelqueue_put(chan->queue, data, lock) != 0) {
goto done;
}

Expand Down Expand Up @@ -1285,7 +1298,8 @@ _channel_destroy(_channels *channels, int64_t id)
}

static int
_channel_send(_channels *channels, int64_t id, PyObject *obj)
_channel_send(_channels *channels, int64_t id, PyObject *obj,
PyThread_type_lock lock)
{
PyInterpreterState *interp = _get_current();
if (interp == NULL) {
Expand Down Expand Up @@ -1319,7 +1333,7 @@ _channel_send(_channels *channels, int64_t id, PyObject *obj)
}

// Add the data to the channel.
int res = _channel_add(chan, PyInterpreterState_GetID(interp), data);
int res = _channel_add(chan, PyInterpreterState_GetID(interp), data, lock);
PyThread_release_lock(mutex);
if (res != 0) {
_PyCrossInterpreterData_Release(data);
Expand Down Expand Up @@ -2337,7 +2351,7 @@ channel_send(PyObject *self, PyObject *args, PyObject *kwds)
return NULL;
}

if (_channel_send(&_globals.channels, cid, obj) != 0) {
if (_channel_send(&_globals.channels, cid, obj, NULL) != 0) {
return NULL;
}
Py_RETURN_NONE;
Expand All @@ -2361,6 +2375,55 @@ channel_recv(PyObject *self, PyObject *args, PyObject *kwds)
return _channel_recv(&_globals.channels, cid);
}

static PyObject *
channel_send_wait(PyObject *self, PyObject *args, PyObject *kwds)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move this to right after _channel_send() (right before channel_recv()).

Also, how is channel_send(cid, obj) any different than channel_send_wait(cid, obj, 0)? It might make sense to have them be the same function.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least the function name passed to PyArg_ParseTupleAndKeywords for error messages would be different?

{
static char *kwlist[] = {"cid", "obj", "timeout", NULL};
int64_t cid;
PyObject *obj;
int64_t timeout = 0;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&O|l:channel_send_wait",
kwlist, channel_id_converter,
&cid, &obj)) {
return NULL;
}

// Create the lock that will be released when the data is recieved.
PyThread_type_lock lock = PyThread_allocate_lock();
if (_channel_send(&_globals.channels, cid, obj, lock) != 0) {
return NULL;
}

long long microseconds;
if (timeout > 0) {
microseconds = timeout * 1000000;
}
else {
microseconds = -1;
}
PyLockStatus lock_rc;
Py_BEGIN_ALLOW_THREADS
lock_rc = PyThread_acquire_lock_timed(lock, microseconds, 0);
Py_END_ALLOW_THREADS

PyThread_free_lock(lock);
if (lock_rc == PY_LOCK_ACQUIRED) {
Py_RETURN_TRUE;
}
else {
Py_RETURN_FALSE;
}
}

PyDoc_STRVAR(channel_send_wait_doc,
"channel_send_wait(cid, obj, timeout)\n\
\n\
Add the object's data to the channel's queue and wait until it's removed.\n\
\n\
If the timeout is set as:\n\
* <= 0 then wait forever until the object is removed from the queue.\n\
* > 0 then wait until the object is removed or for timeout seconds.");

PyDoc_STRVAR(channel_recv_doc,
"channel_recv(cid) -> obj\n\
\n\
Expand Down Expand Up @@ -2481,6 +2544,8 @@ static PyMethodDef module_functions[] = {
METH_NOARGS, channel_list_all_doc},
{"channel_send", (PyCFunction)(void(*)(void))channel_send,
METH_VARARGS | METH_KEYWORDS, channel_send_doc},
{"channel_send_wait", (PyCFunction)(void (*)(void))channel_send_wait,
METH_VARARGS | METH_KEYWORDS, channel_send_wait_doc},
{"channel_recv", (PyCFunction)(void(*)(void))channel_recv,
METH_VARARGS | METH_KEYWORDS, channel_recv_doc},
{"channel_close", (PyCFunction)(void(*)(void))channel_close,
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy