Message1289

Author batyi
Recipients
Date 2006-11-26.16:22:58
SpamBayes Score
Marked as misclassified
Message-id
In-reply-to
Content
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();
 }
History
Date User Action Args
2008-02-20 17:17:35adminlinkissue1603253 messages
2008-02-20 17:17:35admincreate