Skip to content

Commit c010a11

Browse files
author
Dorian Birraux
committed
Merge pull request #206 in LCL/wolframclientforpython from feature/better-read-write-performances to master
* commit '2c9366cf91fee5c84650f3bf0ab1368291553906': Increment version. Update kernel controller and session class __repr__. Update Read message in WL initkernel.m ExternalEvaluate use Frame. Wrapping evaluation data into a Frame Update code with recent changes in ZeroMQLink.
2 parents 709d785 + 2c9366c commit c010a11

7 files changed

Lines changed: 39 additions & 30 deletions

File tree

PacletInfo.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Paclet[
22
Name -> "WolframClientForPython",
3-
Version -> "1.1.0",
3+
Version -> "1.1.1",
44
MathematicaVersion -> "11.3+",
55
Loading -> Automatic,
66
Extensions -> {}

wolframclient/about.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
__name__ = "wolframclient"
44
__description__ = "A Python library with various tools to interact with the Wolfram Language and the Wolfram Cloud."
5-
__version__ = "1.1.0"
5+
__version__ = "1.1.1"
66
__author__ = "Wolfram Research"
77
__author_email__ = "support@wolfram.com, dorianb@wolfram.com, riccardod@wolfram.com"

wolframclient/evaluation/kernel/initkernel.m

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,24 @@ no matter the total size (tested with 80MB) *)
2121

2222
Begin["`Private`"];
2323

24-
{SocketWriteByteArrayFunc, SocketReadByteArrayFunc} = If[
24+
(*Define the most efficient pair of write bytearray and non-blocking read, for various WL versions. *)
25+
{SocketWriteByteArrayFunc, SocketReadByteArrayFuncNoWait} = If[
2526
$VersionNumber < 12,
26-
iSocketWriteByteArray[socket_,ba_ByteArray] := ZeroMQLink`Private`ZMQWriteInternal[socket, Normal[ba]];
27-
iSocketReadByteArray[uuid_String, flags_Integer]:= ByteArray@iRecvSingleMultipartMessageSocket[uuid, flags];
28-
{iSocketWriteByteArray, iSocketReadByteArray}
27+
{
28+
Function[{socketOut, ba}, ZeroMQLink`Private`ZMQWriteInternal[socketOut, Normal[ba]]],
29+
Function[{socketIn},
30+
Block[
31+
{data=ByteArray@iRecvSingleMultipartMessageSocket[First@socketIn, 1(*Flag NOWAIT*)]},
32+
If[Length[data] >= 3, Part[data,4;;], {}]
33+
]
34+
]
35+
}
2936
,
30-
{ZMQSocketWriteMessage, iRecvSingleMultipartBinaryMessageSocket}
37+
{
38+
ZMQSocketWriteMessage,
39+
SocketReadMessage[#, "Blocking"->False] &
40+
}
41+
3142
];
3243

3344
$DEBUG=1;
@@ -169,14 +180,13 @@ no matter the total size (tested with 80MB) *)
169180
$VersionNumber < $TaskSupportMinVersion,
170181
(* Low CPU wait but need synchronous loop. *)
171182
evaluationLoop[socketIn_SocketObject]:= With[
172-
{maxPause=$MaxIdlePause, minPause=$MinIdlePause, incr=$PauseIncrement,
173-
uuidIn=First@socketIn, poller={socketIn}},
183+
{maxPause=$MaxIdlePause, minPause=$MinIdlePause, incr=$PauseIncrement, poller={socketIn}},
174184
Block[{msg},
175185
SendAck[];
176186
While[True,
177-
msg = SocketReadByteArrayFunc[uuidIn, 1 (* NOWAIT *)];
178-
If[Length[msg]>3,
179-
socketEventHandler[msg[[4;;]]];
187+
msg = SocketReadByteArrayFuncNoWait[socketIn];
188+
If[Length[msg]>0,
189+
socketEventHandler[msg];
180190
,
181191
SocketWaitNext[poller];
182192
]
@@ -186,14 +196,13 @@ no matter the total size (tested with 80MB) *)
186196
True,
187197
(* Version with fixed asynchronous tasks *)
188198
evaluationLoop[socketIn_SocketObject]:= With[
189-
{maxPause=$MaxIdlePause, minPause=$MinIdlePause, incr=$PauseIncrement,
190-
uuidIn=First@socketIn, poller={socketIn}},
199+
{maxPause=$MaxIdlePause, minPause=$MinIdlePause, incr=$PauseIncrement, poller={socketIn}},
191200
$Task = SessionSubmit[
192201
ScheduledTask[
193202
(
194-
msg = SocketReadByteArrayFunc[uuidIn, 1 (* NOWAIT *)];
195-
If[Length[msg]>3,
196-
socketEventHandler[msg[[4;;]]];
203+
msg = SocketReadByteArrayFuncNoWait[socketIn];
204+
If[Length[msg]>0,
205+
socketEventHandler[msg];
197206
,
198207
SocketWaitNext[poller];
199208
]

wolframclient/evaluation/kernel/kernelcontroller.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ def evaluate_future(self, wxf, future, result_update_callback=None, **kwargs):
498498

499499
def _do_evaluate(self, wxf, future, result_update_callback):
500500
start = time.perf_counter()
501-
self.kernel_socket_out.send(wxf)
501+
self.kernel_socket_out.send(zmq.Frame(wxf))
502502
if logger.isEnabledFor(logging.DEBUG):
503503
logger.debug("Expression sent to kernel in %.06fsec", time.perf_counter() - start)
504504
start = time.perf_counter()
@@ -592,14 +592,17 @@ def _cancel_tasks(self):
592592

593593
def __repr__(self):
594594
if self.started:
595-
return "<%s: pid:%i, kernel sockets: (in:%s, out:%s)>" % (
595+
return '<%s[%s🔵], "%s", pid:%i, kernel sockets: (in:%s, out:%s)>'\
596+
% (
596597
self.__class__.__name__,
598+
self.name,
599+
self.kernel,
597600
self.kernel_proc.pid,
598601
self.kernel_socket_in.uri,
599602
self.kernel_socket_out.uri,
600603
)
601604
else:
602-
return "<%s: %s>" % (self.__class__.__name__, self.name)
605+
return '<%s[%s🔴], "%s">' % (self.__class__.__name__, self.name, self.kernel)
603606

604607

605608
class _StartEvent(object):

wolframclient/evaluation/kernel/localsession.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -283,10 +283,7 @@ def set_parameter(self, parameter_name, parameter_value):
283283
set_parameter.__doc__ = WolframKernelController.set_parameter.__doc__
284284

285285
def __repr__(self):
286-
if self.started:
287-
return "<%s: kernel controller=%s>" % (
288-
self.__class__.__name__,
289-
self.kernel_controller,
290-
)
291-
else:
292-
return "<%s: not started>" % self.__class__.__name__
286+
return "<%s: %s>" % (
287+
self.__class__.__name__,
288+
self.kernel_controller,
289+
)

wolframclient/utils/api.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464

6565
zmq = API(
6666
Context="zmq.Context",
67+
Frame="zmq.Frame",
6768
PUSH="zmq.PUSH",
6869
PULL="zmq.PULL",
6970
PAIR="zmq.PAIR",

wolframclient/utils/externalevaluate.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,7 @@ def __init__(self, socket):
9090
self.socket = socket
9191

9292
def write(self, bytes):
93-
self.socket.send(bytes)
94-
93+
self.socket.send(zmq.Frame(bytes))
9594

9695
class StdoutProxy:
9796

@@ -159,7 +158,7 @@ def handle_message(socket, evaluate_message=evaluate_message, consumer=None):
159158

160159
__traceback_hidden_variables__ = True
161160

162-
message = binary_deserialize(socket.recv(), consumer=consumer)
161+
message = binary_deserialize(socket.recv(copy=False).buffer, consumer=consumer)
163162
result = evaluate_message(**message)
164163

165164
sys.stdout.flush()

0 commit comments

Comments
 (0)