File: multiproc.py

package info (click to toggle)
python-pyepics 3.4.1%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,080 kB
  • sloc: python: 11,184; makefile: 106; javascript: 104; sh: 1
file content (49 lines) | stat: -rw-r--r-- 1,282 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env python
"""
Provides CAProcess, a multiprocessing.Process that correctly handles Channel Access
and CAPool, pool of CAProcesses

Use CAProcess in place of multiprocessing.Process if your process will be calling
Channel Access or using Epics process variables

   from epics import (CAProcess, CAPool)

"""
#
# Author:         Ken Lauer
# Created:        Feb. 27, 2014
# Modifications:  Matt Newville, changed to subclass multiprocessing.Process
#                 3/28/2014  KL, added CAPool

import multiprocessing as mp
from multiprocessing.pool import Pool
from . import ca
from .ca import clear_cache


__all__ = ['CAProcess', 'CAPool', 'clear_ca_cache']

class CAProcess(mp.Process):
    """
    A Channel-Access aware (and safe) subclass of multiprocessing.Process

    Use CAProcess in place of multiprocessing.Process if your Process will
    be doing CA calls!
    """
    def __init__(self, **kws):
        mp.Process.__init__(self, **kws)

    def run(self):
        ca.initial_context = None
        clear_cache()
        mp.Process.run(self)


class CAPool(Pool):
    """
    An EPICS-aware multiprocessing.Pool of CAProcesses.
    """
    def __init__(self, *args, **kwargs):
        self.Process = CAProcess

        Pool.__init__(self, *args, **kwargs)