Commit b263ba7
committed
Offer a prepare_subprocess_call to make a func multiprocessing safe
When multiprocessing uses another start method different of 'fork', it
requires that the target function and its arguments can be pickled.
In particular, multiprocessing needs to pickle them in the parent
process and un-pickle them in the child process.
Due how pickling works in Python, pickling a function only involves
storing the information needed to reload it: the bytecode is never
stored.
This makes pickling particular tricky in byexample: if a
we want to call a function that it is from one of the byexample
modules/plugins, the pickling will fail.
It will not fail when multiprocessing serialize it (dumps) but when it
deserialize it (loads) because in the child process, the byexample
modules/plugins will not be loaded in sys.modules and because they are
not in the sys.path (in principle), Python will not be able to find
them.
_prepare_subprocess_call() can wrap the target function and its arguments
and replacing the target by a _subprocess_trampoline function that will
call the former.
This _subprocess_trampoline will do all the bootstraping needed in the
child process, including the (re)loading of the modules/plugins, to make
the un-pickling work.
Most of these details are hidden from the user (plugin developer). He/she
is only required to call prepare_subprocess_call() and use the returned
target/arguments in replace of his/her.
The prepare_subprocess_call() is a partial bound function of
_prepare_subprocess_call(). The former can be obtained optionally from
the extension constructor (__init__ method of ExampleParser,
ExampleFinder, ExampleRunner, ZoneDelimiter and Concern).
The function is thread-safe and it will accessible both in the main
byexample process and in each worker thread.1 parent 83e1496 commit b263ba7
2 files changed
Lines changed: 90 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
22 | 25 | | |
23 | 26 | | |
24 | 27 | | |
| |||
39 | 42 | | |
40 | 43 | | |
41 | 44 | | |
42 | | - | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
43 | 49 | | |
44 | 50 | | |
45 | 51 | | |
| |||
58 | 64 | | |
59 | 65 | | |
60 | 66 | | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
61 | 70 | | |
62 | 71 | | |
63 | 72 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
| 2 | + | |
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| |||
627 | 627 | | |
628 | 628 | | |
629 | 629 | | |
| 630 | + | |
| 631 | + | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
630 | 645 | | |
631 | 646 | | |
632 | 647 | | |
| |||
692 | 707 | | |
693 | 708 | | |
694 | 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 | + | |
695 | 774 | | |
696 | 775 | | |
697 | 776 | | |
| |||
0 commit comments