1 package org.python.modules; 3 import org.python.core.*; 4 5 class FunctionThread extends Thread 6 { 7 PyObject func; 8 PyObject[] args; 9 PySystemState systemState; 10 11 public FunctionThread(PyObject func, PyObject[] args) { 12 super(); 13 this.func = func; 14 this.args = args; 15 this.systemState = Py.getSystemState(); 16 } 17 18 public void run() { 19 Py.setSystemState(systemState); 20 try { 21 func.__call__(args); 22 } catch (PyException exc) { 23 Py.printException(exc); 24 } 25 } 26 } 27 28 public class thread implements ClassDictInit 29 { 30 public static PyString __doc__ = new PyString( 31 "This module provides primitive operations to write multi-threaded "+ 32 "programs.\n" + 33 "The 'threading' module provides a more convenient interface." 34 ); 35 36 public static void classDictInit(PyObject dict) { 37 dict.__setitem__("LockType", PyType.fromClass(PyLock.class)); 38 } 39 40 public static PyObject error = new PyString("thread.error"); 41 42 public static void start_new_thread(PyObject func, PyTuple args) { 43 Thread pt = new FunctionThread(func, args.getArray()); 44 pt.start(); 45 } 46 47 public static PyLock allocate_lock() { 48 return new PyLock(); 49 } 50 51 public static void exit() { 52 exit_thread(); 53 } 54 55 public static void exit_thread() { 56 throw new PyException(Py.SystemExit, new PyInteger(0)); 57 } 58 59 public static long get_ident() { 60 return Py.java_obj_id(Thread.currentThread()); 61 } 62 } 63 | Popular Tags |