SubprocessTao
pytao.SubprocessTao
SubprocessTao(*args, env=None, cwd=None, **kwargs)
Bases: Tao
Subprocess helper for Tao.
This special version of the Tao class executes a Python subprocess which
interacts with Tao through ctypes.
This can be used exactly as the normal Tao object with the primary added
benefit that Fortran crashes will not affect the main Python process.
For full parameter information, see the Tao class documentation.
Usage
When creating many SubprocessTao objects, ensure to close the subprocess
when done with it. This can be done manually:
>>> tao.close_subprocess()
Or automatically by way of a context manager:
>>> with SubprocessTao(init_file="$ACC_ROOT_DIR/bmad-doc/tao_examples/cbeta_cell/tao.init", plot=True) as tao:
... tao.plot("floor")
To add a new environment variable in addition to the parent process environment:
>>> import os
>>> with SubprocessTao(init_file="...", env={**os.environ, "NEW_VAR": "NEW_VALUE"}) as tao:
... print(tao.version())
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
dict[str, str] or None
|
Environment variables to use for the subprocess. If None, defaults to
|
None
|
cwd
|
Path or str
|
The current working directory to use when spawning the subprocess.
Any filenames used in initializing Tao will be relative to this.
Supports environment variables and home directory ( |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
subprocess_env |
dict[str, str]
|
Environment variables to use for the subprocess. It is recommended to
use a new |
Source code in pytao/subproc.py
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 | |
Attributes
pytao.SubprocessTao.subprocess_alive
property
subprocess_alive
Subprocess is still running.
Methods:
pytao.SubprocessTao.close_subprocess
close_subprocess(*, force=False)
Close the Tao subprocess.
Source code in pytao/subproc.py
614 615 616 617 618 619 620 621 | |
pytao.SubprocessTao.cmd
cmd(cmd, raises=True)
Runs a command, and returns the output.
Source code in pytao/subproc.py
796 797 798 799 800 | |
pytao.SubprocessTao.cmd_integer
cmd_integer(cmd, raises=True)
Runs a command, and returns an integer array.
Source code in pytao/subproc.py
810 811 812 813 814 815 816 | |
pytao.SubprocessTao.cmd_real
cmd_real(cmd, raises=True)
Runs a command, and returns a floating point array.
Source code in pytao/subproc.py
802 803 804 805 806 807 808 | |
pytao.SubprocessTao.fork_map
fork_map(func, items, *, max_workers=None, timeout=None, raises=True, omp_num_threads=1)
Run func(tao, item) for each item in a forked copy of the Tao subprocess.
Each child starts from a copy of the current Tao state, so nothing is re-initialized and changes in one child never affect another or this object. Only the subprocess is forked, never the calling process.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
callable
|
Called as |
required |
items
|
iterable
|
Items to process. Results keep this order. |
required |
max_workers
|
int
|
Concurrent children. Defaults to the CPU count; negative values
count back from it ( |
None
|
timeout
|
float
|
Overall limit in seconds. On expiry all children are killed and
|
None
|
raises
|
bool
|
If True, the first failure raises |
True
|
omp_num_threads
|
int or None
|
OpenMP threads each child may use. |
1
|
Returns:
| Type | Description |
|---|---|
list
|
One entry per item, in input order. |
Raises:
| Type | Description |
|---|---|
TaoForkError
|
A child failed, crashed, or returned an unreadable result. |
TimeoutError
|
|
NotImplementedError
|
No |
Notes
Use noplot=True or external plotting; Tao plot windows are not
fork-safe.
libtao links OpenMP, and a forked child has none of the subprocess's OpenMP worker threads. Children are single-threaded by default, which is safe with every OpenMP runtime and avoids oversubscription. With more threads, a libtao built against GNU libgomp can hang in a child once the subprocess has run a parallel region; LLVM libomp does not.
Examples:
>>> def scan(tao, k1):
... tao.cmd(f"set ele Q1 k1 = {k1}")
... return tao.ele_twiss("Q2")["beta_a"]
>>> tao.fork_map(scan, np.linspace(0.0, 2.0, 21), max_workers=4)
Source code in pytao/subproc.py
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 | |
pytao.SubprocessTao.get_active_beam_track_element
get_active_beam_track_element()
Get the active element index being tracked via shared memory.
Source code in pytao/subproc.py
818 819 820 821 | |
pytao.SubprocessTao.subprocess_call
subprocess_call(func, *args, **kwargs)
Run a custom function in the subprocess.
Any picklable callable works, including a lambda, a closure, a
functools.partial, or a function defined in a notebook or script.
The first argument passed will be the tao object, and the remainder
of the arguments are user-specified by either positional or keyword
argument. The arguments and the return value must be picklable.
Source code in pytao/subproc.py
695 696 697 698 699 700 701 702 703 704 705 | |
pytao.SubprocessTao.timeout
timeout(when)
Context manager to set a timeout for a block of SubprocessTao calls.
Note that there is no possibility for a graceful timeout. In the event of a timeout, the subprocess will be terminated.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
when
|
float
|
The timeout duration in seconds. |
required |
Yields:
| Type | Description |
|---|---|
None
|
Yields control back to the calling context. |
Raises:
| Type | Description |
|---|---|
TimeoutError
|
If the block of code does not execute within |
Source code in pytao/subproc.py
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 | |