Issue1603253

classification
Title: Thread's setDaemon setting does not work
Type: Severity: normal
Components: Library Versions:
Milestone:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: batyi, cgroves, leouserz
Priority: normal Keywords:

Created on 2006-11-26.16:22:58 by batyi, last changed 2007-02-28.22:12:07 by cgroves.

Messages
msg1289 (view) Author: Petr Gladkikh (batyi) Date: 2006-11-26.16:22:58
One can not create "daemon" thread using Jython's threading.Thread class.

Probably simplest code that reveals the problem is (this should be main thread):

from threading import Thread
def worker():
       while 1: pass
thread = Thread(target=worker, name="daemon-thread")
thread.setDaemon(1)
thread.start()

CPython exits almost immediately as expected. Jython hangs indefinitely (in all versions iI have tested - 2.1, 2.2alpha and repository trunk revision 2989).

There is a suggestion in jython-users mailing list
http://sourceforge.net/mailarchive/message.php?msg_id=11041636
regarding this behaviour.

Note that only way to make Java Thread class instance to be daemon is to call "setDaemon()" method (there's no appropriate constuctor arguments to do that). There's also similar restriction in Java and Python: "daemon" status can be set only before a thread is started. So I think the solution that is suggested in message above is correct. I'll copy corrected function here for completeness:

public static void start_new_thread(PyObject func, PyTuple args) {
    Thread pt = new FunctionThread(func, args.list);
    PyObject currentThread = func.__findattr__("im_self");
    if(currentThread != null) {
      PyObject isDaemon = currentThread.__getattr__("isDaemon");
      boolean d = isDaemon.__call__().__nonzero__();
      pt.setDaemon(d);
    }
    pt.start();
 }
msg1290 (view) Author: Deleted User leouserz (leouserz) Date: 2006-12-21.18:25:08
this patch makes sense.  Id also include having the name set as well to the Java thread to match the Python thread.

patch:
--- /org/python/modules/thread.java	Thu Dec 21 12:26:01 2006
+++ org/python/modules/thread.java	Thu Dec 21 12:25:23 2006
@@ -23,6 +23,7 @@
             Py.printException(exc);

         }
 
     }

+
 }

 

 public class thread implements ClassDictInit

@@ -41,6 +42,20 @@
 

     public static void start_new_thread(PyObject func, PyTuple args) {

         Thread pt = new FunctionThread(func, args.getArray());

+        PyObject im_self = func.__findattr__("im_self");
+        if(im_self != null){
+            PyObject isDaemon = im_self.__findattr__("isDaemon");
+            if(isDaemon != null && isDaemon.isCallable()){
+                PyObject po = isDaemon.__call__();
+                if(po.__nonzero__())
+	            pt.setDaemon(true);
+	    }
+            PyObject getName = im_self.__findattr__("getName");
+            if(getName != null && getName.isCallable()){
+                PyObject pname = getName.__call__();
+                pt.setName(String.valueOf(pname));
+            }
+        }
         pt.start();

     }

 



leouser
msg1291 (view) Author: Charlie Groves (cgroves) Date: 2007-02-28.22:12:07
Committed in r3131
History
Date User Action Args
2006-11-26 16:22:58batyicreate