ipc
Classes and utilities for communication between Executors and subprocesses.
Communicators manage message passing and parsing between subprocesses. They maintain a limited public interface of "read" and "write" operations. Behind this interface the methods of communication vary from serialization across pipes to Unix sockets, etc. All communicators pass a single object called a "Message" which contains an arbitrary "contents" field as well as an optional "signal" field.
Classes:
Name | Description |
---|---|
Party |
Enum describing whether Communicator is on Task-side or Executor-side. |
Message |
A dataclass used for passing information from Task to Executor. |
Communicator |
Abstract base class for Communicator types. |
PipeCommunicator |
Manages communication between Task and Executor via pipes (stderr and stdout). |
SocketCommunicator |
Manages communication using sockets, either raw or using zmq. Supports both TCP and Unix sockets. |
Communicator
Bases: ABC
Source code in lute/execution/ipc.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
|
has_messages: bool
property
Whether the Communicator has remaining messages.
The precise method for determining whether there are remaining messages will depend on the specific Communicator sub-class.
__init__(party=Party.TASK, use_pickle=True)
Abstract Base Class for IPC Communicator objects.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
party
|
Party
|
Which object (side/process) the Communicator is managing IPC for. I.e., is this the "Task" or "Executor" side. |
TASK
|
use_pickle
|
bool
|
Whether to serialize data using pickle prior to sending it. |
True
|
Source code in lute/execution/ipc.py
106 107 108 109 110 111 112 113 114 115 116 117 |
|
clear_communicator()
Alternative exit method outside of context manager.
Source code in lute/execution/ipc.py
154 155 156 |
|
delayed_setup()
Any setup that should be done later than init.
Source code in lute/execution/ipc.py
158 159 160 |
|
read(proc)
abstractmethod
Method for reading data through the communication mechanism.
Source code in lute/execution/ipc.py
119 120 121 122 |
|
stage_communicator()
Alternative method for staging outside of context manager.
Source code in lute/execution/ipc.py
150 151 152 |
|
write(msg)
abstractmethod
Method for sending data through the communication mechanism.
Source code in lute/execution/ipc.py
124 125 126 127 |
|
Party
Bases: Enum
Identifier for which party (side/end) is using a communicator.
For some types of communication streams there may be different interfaces depending on which side of the communicator you are on. This enum is used by the communicator to determine which interface to use.
Source code in lute/execution/ipc.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
|
EXECUTOR = 1
class-attribute
instance-attribute
The Executor (server) side.
TASK = 0
class-attribute
instance-attribute
The Task (client) side.
PipeCommunicator
Bases: Communicator
Provides communication through pipes over stderr/stdout.
The implementation of this communicator has reading and writing ocurring
on stderr and stdout. In general the Task
will be writing while the
Executor
will be reading. stderr
is used for sending signals.
Source code in lute/execution/ipc.py
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 |
|
__init__(party=Party.TASK, use_pickle=True)
IPC through pipes.
Arbitrary objects may be transmitted using pickle to serialize the data. If pickle is not used
Parameters:
Name | Type | Description | Default |
---|---|---|---|
party
|
Party
|
Which object (side/process) the Communicator is managing IPC for. I.e., is this the "Task" or "Executor" side. |
TASK
|
use_pickle
|
bool
|
Whether to serialize data using Pickle prior to sending it. If False, data is assumed to be text whi |
True
|
Source code in lute/execution/ipc.py
171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
|
_safe_unpickle_decode(maybe_mixed)
This method is used to unpickle and/or decode a bytes object.
It attempts to handle cases where contents can be mixed, i.e., part of
the message must be decoded and the other part unpickled. It handles
only two-way splits. If there are more complex arrangements such as:
The simpler two way splits are unlikely to occur in normal usage. They
may arise when debugging if, e.g., print
statements are mixed with the
usage of the _report_to_executor
method.
Note that this method works because ONLY text data is assumed to be
sent via the pipes. The method needs to be revised to handle non-text
data if the Task
is modified to also send that via PipeCommunicator.
The use of pickle is supported to provide for this option if it is
necessary. It may be deprecated in the future.
Be careful when making changes. This method has seemingly redundant checks because unpickling will not throw an error if a full object can be retrieved. That is, the library will ignore extraneous bytes. This method attempts to retrieve that information if the pickled data comes first in the stream.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
maybe_mixed
|
bytes
|
A bytes object which could require unpickling, decoding, or both. |
required |
Returns:
Name | Type | Description |
---|---|---|
contents |
Optional[str]
|
The unpickled/decoded contents if possible. Otherwise, None. |
Source code in lute/execution/ipc.py
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
|
read(proc)
Read from stdout and stderr.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
proc
|
Popen
|
The process to read from. |
required |
Returns:
Name | Type | Description |
---|---|---|
msg |
Message
|
The message read, containing contents and signal. |
Source code in lute/execution/ipc.py
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
|
write(msg)
Write to stdout and stderr.
The signal component is sent to stderr
while the contents of the
Message are sent to stdout
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
msg
|
Message
|
The Message to send. |
required |
Source code in lute/execution/ipc.py
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 |
|
SocketCommunicator
Bases: Communicator
Provides communication over Unix or TCP sockets.
Communication is provided either using sockets with the Python socket library
or using ZMQ. The choice of implementation is controlled by the global bool
USE_ZMQ
.
Whether to use TCP or Unix sockets is controlled by the environment
LUTE_USE_TCP=1
If defined, TCP sockets will be used, otherwise Unix sockets will be used.
Regardless of socket type, the environment variable
LUTE_EXECUTOR_HOST=<hostname>
will be defined by the Executor-side Communicator.
For TCP sockets:
The Executor-side Communicator should be run first and will bind to all
interfaces on the port determined by the environment variable:
LUTE_PORT=###
If no port is defined, a port scan will be performed and the Executor-side
Communicator will bind the first one available from a random selection. It
will then define the environment variable so the Task-side can pick it up.
For Unix sockets:
The path to the Unix socket is defined by the environment variable:
LUTE_SOCKET=/path/to/socket
This class assumes proper permissions and that this above environment
variable has been defined. The Task
is configured as what would commonly
be referred to as the client
, while the Executor
is configured as the
server.
If the Task process is run on a different machine than the Executor, the
Task-side Communicator will open a ssh-tunnel to forward traffic from a local
Unix socket to the Executor Unix socket. Opening of the tunnel relies on the
environment variable:
LUTE_EXECUTOR_HOST=<hostname>
to determine the Executor's host. This variable should be defined by the
Executor and passed to the Task process automatically, but it can also be
defined manually if launching the Task process separately. The Task will use
the local socket <LUTE_SOCKET>.task{##}
. Multiple local sockets may be
created. Currently, it is assumed that the user is identical on both the Task
machine and Executor machine.
Source code in lute/execution/ipc.py
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 |
|
ACCEPT_TIMEOUT: float = 0.01
class-attribute
instance-attribute
Maximum time to wait to accept connections. Used by Executor-side.
MSG_HEAD: bytes = b'MSG'
class-attribute
instance-attribute
Start signal of a message. The end of a message is indicated by MSG_HEAD[::-1].
MSG_SEP: bytes = b';;;'
class-attribute
instance-attribute
Separator for parts of a message. Messages have a start, length, message and end.
__init__(party=Party.TASK, use_pickle=True)
IPC over a TCP or Unix socket.
Unlike with the PipeCommunicator, pickle is always used to send data through the socket.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
party
|
Party
|
Which object (side/process) the Communicator is managing IPC for. I.e., is this the "Task" or "Executor" side. |
TASK
|
use_pickle
|
bool
|
Whether to use pickle. Always True currently, passing False does not change behaviour. |
True
|
Source code in lute/execution/ipc.py
401 402 403 404 405 406 407 408 409 410 411 412 413 414 |
|
_clean_up()
Clean up connections.
Source code in lute/execution/ipc.py
949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 |
|
_create_socket_raw()
Create either a Unix or TCP socket.
If the environment variable
LUTE_USE_TCP=1
is defined, a TCP socket is returned, otherwise a Unix socket.
Refer to the individual initialization methods for additional environment variables controlling the behaviour of these two communication types.
Returns:
Name | Type | Description |
---|---|---|
data_socket |
socket
|
TCP or Unix socket. |
Source code in lute/execution/ipc.py
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 |
|
_create_socket_zmq()
Create either a Unix or TCP socket.
If the environment variable
LUTE_USE_TCP=1
is defined, a TCP socket is returned, otherwise a Unix socket.
Refer to the individual initialization methods for additional environment variables controlling the behaviour of these two communication types.
Returns:
Name | Type | Description |
---|---|---|
data_socket |
socket
|
Unix socket object. |
Source code in lute/execution/ipc.py
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 |
|
_find_random_port(min_port=41923, max_port=64324, max_tries=100)
Find a random open port to bind to if using TCP.
Source code in lute/execution/ipc.py
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 |
|
_get_socket_path()
Return the socket path, defining one if it is not available.
Returns:
Name | Type | Description |
---|---|---|
socket_path |
str
|
Path to the Unix socket. |
Source code in lute/execution/ipc.py
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 |
|
_init_tcp_socket_raw()
Initialize a TCP socket.
Executor-side code should always be run first. It checks to see if
the environment variable
LUTE_PORT=###
is defined, if so binds it, otherwise find a free port from a selection
of random ports. If a port search is performed, the LUTE_PORT
variable
will be defined so it can be picked up by the the Task-side Communicator.
In the event that no port can be bound on the Executor-side, or the port and hostname information is unavailable to the Task-side, the program will exit.
Returns:
Name | Type | Description |
---|---|---|
data_socket |
socket
|
TCP socket object. |
Source code in lute/execution/ipc.py
722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 |
|
_init_tcp_socket_zmq(data_socket)
Initialize a TCP socket using ZMQ.
Equivalent as the method above but requires passing in a ZMQ socket object instead of returning one.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data_socket
|
Socket
|
Socket object. |
required |
Source code in lute/execution/ipc.py
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 |
|
_init_unix_socket_raw()
Returns a Unix socket object.
Executor-side code should always be run first. It checks to see if
the environment variable
LUTE_SOCKET=XYZ
is defined, if so binds it, otherwise it will create a new path and
define the environment variable for the Task-side to find.
On the Task (client-side), this method will also open a SSH tunnel to forward a local Unix socket to an Executor Unix socket if the Task and Executor processes are on different machines.
Returns:
Name | Type | Description |
---|---|---|
data_socket |
socket
|
Unix socket object. |
Source code in lute/execution/ipc.py
833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 |
|
_init_unix_socket_zmq(data_socket)
Initialize a Unix socket object, using ZMQ.
Equivalent as the method above but requires passing in a ZMQ socket object instead of returning one.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data_socket
|
socket
|
ZMQ object. |
required |
Source code in lute/execution/ipc.py
879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 |
|
_read_socket()
Read data from a socket.
Socket(s) are continuously monitored, and read from when new data is available.
Calls an underlying method for either raw sockets or ZMQ.
Source code in lute/execution/ipc.py
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 |
|
_read_socket_raw()
Read data from a socket.
Raw socket implementation for the reader thread.
Source code in lute/execution/ipc.py
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 |
|
_read_socket_zmq()
Read data from a socket.
ZMQ implementation for the reader thread.
Source code in lute/execution/ipc.py
516 517 518 519 520 521 522 523 524 525 |
|
_setup_unix_ssh_tunnel(socket_path, hostname, executor_hostname)
Prepares an SSH tunnel for forwarding between Unix sockets on two hosts.
An SSH tunnel is opened with ssh -L <local>:<remote> sleep 2
.
This method of communication is slightly slower and incurs additional
overhead - it should only be used as a backup. If communication across
multiple hosts is required consider using TCP. The Task will use
the local socket <LUTE_SOCKET>.task{##}
. Multiple local sockets may be
created. It is assumed that the user is identical on both the
Task machine and Executor machine.
Returns:
Name | Type | Description |
---|---|---|
local_socket_path |
str
|
The local Unix socket to connect to. |
Source code in lute/execution/ipc.py
911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 |
|
_unpack_messages(data)
Unpacks a byte stream into individual messages.
The items between <> are replaced as follows: -
: A start marker -Partial messages (a series of bytes which cannot be converted to a full message) are stored for later. An attempt is made to reconstruct the message with the next call to this method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
bytes
|
A raw byte stream containing anywhere from a partial message to multiple full messages. |
required |
Source code in lute/execution/ipc.py
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 |
|
_write_socket(msg)
Sends data over a socket from the 'client' (Task) side.
The items between <> are replaced as follows: -
: A start marker -This structure is used for decoding the message on the other end.
Source code in lute/execution/ipc.py
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 |
|
delayed_setup()
Delays the creation of socket objects.
The Executor initializes the Communicator when it is created. Since all Executors are created and available at once we want to delay acquisition of socket resources until a single Executor is ready to use them.
Source code in lute/execution/ipc.py
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 |
|
read(proc)
Return a message from the queue if available.
Socket(s) are continuously monitored, and read from when new data is available.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
proc
|
Popen
|
The process to read from. Provided for compatibility with other Communicator subtypes. Is ignored. |
required |
Returns:
Name | Type | Description |
---|---|---|
msg |
Message
|
The message read, containing contents and signal. |
Source code in lute/execution/ipc.py
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 |
|
write(msg)
Send a single Message.
The entire Message (signal and contents) is serialized and sent through a connection over Unix socket.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
msg
|
Message
|
The Message to send. |
required |
Source code in lute/execution/ipc.py
618 619 620 621 622 623 624 625 626 627 |
|