- GIL. , threading.Thread a multiprocessing.Process:
class myThread (multiprocessing.Process):
def __init__(self, threadID, name, cont):
super(myThread, self).__init__()
self.threadID = threadID
self.name = name
self.cont = contdef run(self):
print "Starting " + self.name +"\n"
char = getch.getch()
print 'You pressed %s' % char
cont.append(1)
print "Terminating" + self.name
cont = []
thread1 = myThread(1, "Thread1", cont)
thread1.start()
while cont == []:
print "Running"
time.sleep(0.5)
:
dan@dantop:~$ ./get.py
Running
Starting Thread1
Running
Running
Running
Running
Running
Running
Running
Running
You pressed f
TerminatingThread1
Running
Running
Running
Running
Running
Running
Running
Running
getch C- getchar(), GIL. Python , , getchar().
, GIL getch C-extension, Py_BEGIN_THREADS Py_ALLOW_THREADS:
static PyObject *getch_getche(PyObject *self, PyObject *args)
{
int ok = PyArg_ParseTuple(args, "");
char c;
Py_BEGIN_ALLOW_THREADS
c = getche();
Py_END_ALLOW_THREADS
return PyUnicode_FromFormat("%c", c);
}
static PyObject *getch_getch(PyObject *self, PyObject *args)
{
int ok = PyArg_ParseTuple(args, "");
char c;
Py_BEGIN_ALLOW_THREADS
c = getch();
Py_END_ALLOW_THREADS
return PyUnicode_FromFormat("%c", c);
}
getchmodule.c , , .