Skip to content

Commit 1d32002

Browse files
author
philoinovsky
committed
fix multiple issues
change post([f]{f()}) to post(f) fix closing namespace comment add TODO for windows socket remove iostream include add GIL release and acuiqre in completion handlers
1 parent a2e1238 commit 1d32002

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

include/boost/python/eventloop.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ class event_loop
3737
// TODO: An instance of asyncio.Handle is returned, which can be used later to cancel the callback.
3838
inline void call_soon(object f)
3939
{
40-
_strand.post([f]{f();});
40+
_strand.post([f]{
41+
PyEval_AcquireLock();
42+
f();
43+
PyEval_ReleaseLock();
44+
});
4145
}
4246

4347
// TODO: implement this
@@ -170,6 +174,6 @@ class event_loop
170174

171175
void set_default_event_loop(const boost::asio::io_context::strand& strand);
172176

173-
}}} // namespace boost::python
177+
}}} // namespace boost::python::asio
174178

175179
# endif

src/eventloop.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
// 4. _ensure_resolve
1111

1212
#include <errno.h>
13-
#include <iostream>
1413
#include <boost/asio.hpp>
1514
#include <boost/bind.hpp>
1615
#include <boost/python.hpp>
@@ -109,7 +108,11 @@ void event_loop::call_later(double delay, object f)
109108
_strand.context(),
110109
std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::duration<double>(delay)));
111110
p_timer->async_wait(boost::asio::bind_executor(_strand,
112-
[f, p_timer] (const boost::system::error_code& ec) {f();}));
111+
[f, p_timer] (const boost::system::error_code& ec) {
112+
PyEval_AcquireLock();
113+
f();
114+
PyEval_ReleaseLock();
115+
}));
113116
}
114117

115118
void event_loop::call_at(double when, object f)
@@ -122,6 +125,7 @@ void event_loop::call_at(double when, object f)
122125
[f, p_timer] (const boost::system::error_code& ec) {f();}));
123126
}
124127

128+
// TODO: support windows socket
125129
object event_loop::sock_recv(object sock, size_t nbytes)
126130
{
127131
int fd = extract<int>(sock.attr("fileno")());
@@ -131,14 +135,17 @@ object event_loop::sock_recv(object sock, size_t nbytes)
131135
object py_fut = create_future();
132136
_async_wait_fd(fd_dup,
133137
[py_fut, nbytes, fd=fd_dup] {
138+
PyEval_AcquireLock();
134139
std::vector<char> buffer(nbytes);
135140
read(fd, buffer.data(), nbytes);
136141
py_fut.attr("set_result")(object(handle<>(PyBytes_FromStringAndSize(buffer.data(), nbytes))));
142+
PyEval_ReleaseLock();
137143
},
138144
_read_key(fd));
139145
return py_fut;
140146
}
141147

148+
// TODO: support windows socket
142149
object event_loop::sock_recv_into(object sock, object buffer)
143150
{
144151
int fd = extract<int>(sock.attr("fileno")());
@@ -149,14 +156,17 @@ object event_loop::sock_recv_into(object sock, object buffer)
149156
object py_fut = create_future();
150157
_async_wait_fd(fd_dup,
151158
[py_fut, nbytes, fd=fd_dup] {
159+
PyEval_AcquireLock();
152160
std::vector<char> buffer(nbytes);
153161
ssize_t nbytes_read = read(fd, buffer.data(), nbytes);
154162
py_fut.attr("set_result")(nbytes_read);
163+
PyEval_ReleaseLock();
155164
},
156165
_read_key(fd));
157166
return py_fut;
158167
}
159168

169+
// TODO: support windows socket
160170
object event_loop::sock_sendall(object sock, object data)
161171
{
162172
int fd = extract<int>(sock.attr("fileno")());
@@ -168,13 +178,16 @@ object event_loop::sock_sendall(object sock, object data)
168178
object py_fut = create_future();
169179
_async_wait_fd(fd_dup,
170180
[py_fut, fd, py_str, py_str_len] {
181+
PyEval_AcquireLock();
171182
write(fd, py_str, py_str_len);
172183
py_fut.attr("set_result")(object());
184+
PyEval_ReleaseLock();
173185
},
174186
_write_key(fd));
175187
return py_fut;
176188
}
177189

190+
// TODO: support windows socket
178191
object event_loop::sock_connect(object sock, object address)
179192
{
180193

@@ -243,8 +256,10 @@ object event_loop::getaddrinfo(object host, int port, int family, int type, int
243256
object py_fut = create_future();
244257
_strand.post(
245258
[this, py_fut, host, port, family, type, proto, flags] {
259+
PyEval_AcquireLock();
246260
object res = _pymod_socket.attr("getaddrinfo")(host, port, family, type, proto, flags);
247261
py_fut.attr("set_result")(res);
262+
PyEval_ReleaseLock();
248263
});
249264
return py_fut;
250265
}
@@ -254,8 +269,10 @@ object event_loop::getnameinfo(object sockaddr, int flags)
254269
object py_fut = create_future();
255270
_strand.post(
256271
[this, py_fut, sockaddr, flags] {
272+
PyEval_AcquireLock();
257273
object res = _pymod_socket.attr("getnameinfo")(sockaddr, flags);
258274
py_fut.attr("set_result")(res);
275+
PyEval_ReleaseLock();
259276
});
260277
return py_fut;
261278
}
@@ -455,4 +472,4 @@ void set_default_event_loop(const boost::asio::io_context::strand& strand)
455472
asyncio.attr("set_event_loop_policy")(boost_policy_instance);
456473
}
457474

458-
}}} // namespace boost::python
475+
}}} // namespace boost::python::asio

0 commit comments

Comments
 (0)