Bugs item #1175202, was opened at 2005-04-02 07:24 Message generated for change (Comment added) made by loewis You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1175202&group_id=5470
Category: Threads Group: Python 2.4 >Status: Closed >Resolution: Wont Fix Priority: 5 Submitted By: Jeff Stearns (jeffstearns) Assigned to: Nobody/Anonymous (nobody) Summary: python hangs if import statement launches threads Initial Comment: (This bug looks similar to bug 1175194, but reports a different problem.) I have a simple module which launches multiple HTTP client threads. The main thread creates 10 HTTP clients, each of which fetches documents from a web server. The main thread then simply goes to sleep while the client threads work. The threads are launched when the module is imported. If I launch the script via python bug1.py it launches and runs OK. But if I launch the script via python -c 'import bug1' it runs a bit, then hangs. Here's an example: [EMAIL PROTECTED]> ./python -c 'import bug1' Using 10 threads cccccccccc <- [program hangs here] Each thread prints a character every time that it does something interesting. The 'c' characters indicate that a connect syscall was made. These should be followed by 'C', indicating the the connect returned. That never happens. You might argue that it's inappropriate to launch threads from within import statement, but I can't find a specific prohibition against it. Here's a trace of the last few syscalls before it hangs (pids are actually thread ids): [pid 15481] futex(0x820cc48, FUTEX_WAKE, 1) = 0 [pid 15481] futex(0x8148698, FUTEX_WAIT, 0, NULL <unfinished ...> [pid 15482] futex(0x81a9a80, FUTEX_WAKE, 1) = 0 [pid 15482] futex(0x820cc48, FUTEX_WAKE, 1) = 0 [pid 15482] write(2, "c", 1c) = 1 [pid 15482] futex(0x820cc48, FUTEX_WAKE, 1) = 0 [pid 15482] futex(0x820cc48, FUTEX_WAKE, 1) = 0 [pid 15482] futex(0x8148698, FUTEX_WAIT, 0, NULL <- hangs here Please note that this bug is similar to bug 1175194, but reports a different problem. ---------------------------------------------------------------------- >Comment By: Martin v. Löwis (loewis) Date: 2005-04-03 18:24 Message: Logged In: YES user_id=21627 This is not a bug. The import statement returns when the code of the module completes. In this example, the import statement returns when the call run(10) completes. As the run method has a very long sleep, the import won't return until that sleep is over. So the problem is not that 10 threads are started, but that the module then blocks in its module execution. Furthermore, Python serializes all import statements, with a single import lock. So while one thread performs an import, other threads block when trying to import. So if the threads just started try to import something, they wait until the "main" import completes. If the main imports wait for the threads to complete (i.e. the commented-out thread.join), you get a dead-lock. Closing as "won't fix". ---------------------------------------------------------------------- You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1175202&group_id=5470 _______________________________________________ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com