#!/usr/bin/env python
import time
import sys
try:
    import thread
except:
    import _thread as thread
import subprocess
import tempfile
import shlex

from pcaspy import Driver, SimpleServer

prefix = 'MTEST:'
pvdb = { 
    'COMMAND' : {
        'type' : 'char',
        'count': 128,
        'asyn' : True
    },
    'OUTPUT'  : {
        'type' : 'char',
        'count': 500,
    },
    'STATUS'  : {
        'type' : 'enum',
        'enums': ['DONE', 'BUSY']
    },
    'ERROR'   : {
        'type' : 'string',
    },
}

class myDriver(Driver):
    def __init__(self):
        Driver.__init__(self)
        self.tid = None 

    def write(self, reason, value):
        status = True
        # take proper actions
        if reason == 'COMMAND':
            if not self.tid:
                command = value
                self.tid = thread.start_new_thread(self.runShell,(command,))
            else:
                status = False
        else:
            status = False
        # store the values
        if status:
            self.setParam(reason, value)
        return status

    def runShell(self, command):
        print("DEBUG: Run ", command)
        # set status BUSY
        self.setParam('STATUS', 1)
        self.updatePVs()
        # run shell
        try:
            proc = subprocess.Popen(shlex.split(command), 
                    stdout = subprocess.PIPE, 
                    stderr = subprocess.PIPE)
            proc.wait()
        except OSError:
            self.setParam('ERROR', str(sys.exc_info()[1]))
            self.setParam('OUTPUT', '')
        else:
            self.setParam('ERROR', proc.stderr.read().rstrip())
            self.setParam('OUTPUT', proc.stdout.read().rstrip())
        self.callbackPV('COMMAND')
        # set status DONE
        self.setParam('STATUS', 0)
        self.updatePVs()
        self.tid = None
        print("DEBUG: Finish ", command)

if __name__ == '__main__':
    server = SimpleServer()
    server.createPV(prefix, pvdb)
    driver = myDriver()

    while True:
        # process CA transactions
        server.process(0.1)

@@ -38,25 +38,16 @@
 ort sys%0A
-try:%0A    
 import t
@@ -55,45 +55,11 @@
 read
-%0Aexcept:%0A    import _thread as thread
+ing
 %0Aimp
@@ -476,17 +476,28 @@
    %7D,%0A%7D%0A
+import math
 %0A
-
 class my
@@ -810,26 +810,26 @@
 read
-.start_new_thread(
+ing.Thread(target=
 self
@@ -838,16 +838,21 @@
 unShell,
+args=
 (command
@@ -855,16 +855,49 @@
 mand,))%0A
+                self.tid.start()%0A
         

use threading.Thread because thread is deprecated in Python 3