This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Unsupported provider

classification
Title: Add threading.main_thread() function
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.4
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: asvetlov, christian.heimes, giampaolo.rodola, pitrou, python-dev, r.david.murray, serhiy.storchaka, vstinner
Priority: normal Keywords: patch

Created on 2013-08-30 10:26 by asvetlov, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
issue18882.diff asvetlov, 2013-08-30 12:48 review
issue18882-2.diff asvetlov, 2013-08-31 03:49
issue18882-4.diff asvetlov, 2013-09-01 05:20 review
Messages (16)
msg196521 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2013-08-30 10:26
We need public API for getting main thread object.
See also http://comments.gmane.org/gmane.comp.python.devel/141370
msg196526 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2013-08-30 12:29
The function must take care of fork() in worker threads, too. The isinstance(current_thread(), _MainThread) trick may not work.
msg196529 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2013-08-30 12:47
> The function must take care of fork() in worker threads, too. The
> isinstance(current_thread(), _MainThread) trick may not work.

Well, there are two possibilities:
- main_thread() returns the original _MainThread instance, even if it's
  dead in the child process
- main_thread() returns the main thread of the current process

Both are reasonable, but we must settle for one :-)

(also, the use case of forking from a thread is really obscure, I don't
think we should worry too much about it)
msg196530 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2013-08-30 12:48
Patch with code and tests is attached.
Test fails when program forks from thread other than the main one.
msg196531 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2013-08-30 12:49
signal module reinitializes main_thread variable in PyOS_AfterFork, threading does nothing with forking.
msg196535 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2013-08-30 13:30
http://bugs.python.org/issue16500 is required to make work after fork from thread other than the main one.
msg196540 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2013-08-30 13:56
> http://bugs.python.org/issue16500 is required to make work after
> fork from thread other than the main one.

No it isn't. Please take a look at _after_fork() in threading.py.
msg196615 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2013-08-31 03:49
There is updated patch.
All tests pass.
I've added docs for threading.main_thread() function also.
msg196658 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2013-08-31 17:53
Ok, some comments about the patch (no "review" links appears so I'm gonna do it inline here):

- the doc addition needs a "versionadded" tag

- "The main thread is the thread that the OS creates to run application.": I would rephrase this "In normal conditions, the main thread is the thread from which the Python interpreter was started".

- in the tests:
+        self.assertEqual(data, "Thread-1\nTrue\nTrue\n")

Hmm, how do you know it will be called "Thread-1"?
I would give a specific name to the Thread, so as to make the test deterministic.

+        self.assertEqual(rc, 0)

You don't need this, it is already ensured by assert_python_ok().

- in threading.py, why doesn't _exitfunc() reuse the _main_thread global variable, instead of taking it as a parameter?
msg196707 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2013-09-01 05:20
Uploaded new patch.

> - the doc addition needs a "versionadded" tag
Fixed.

> - "The main thread is the thread that the OS creates to run application.": I would rephrase this "In normal conditions, the main thread is the thread from which the Python interpreter was started".
Fixed.

> - in the tests:
> +        self.assertEqual(data, "Thread-1\nTrue\nTrue\n")
> 
> Hmm, how do you know it will be called "Thread-1"?
> I would give a specific name to the Thread, so as to make the test deterministic.
In this test main thread after forking is always first thread created by python. That's why it always is called 'Thread-1'. 

> +        self.assertEqual(rc, 0)
> 
> You don't need this, it is already ensured by assert_python_ok().
Fixed.

> - in threading.py, why doesn't _exitfunc() reuse the _main_thread global variable, instead of taking it as a parameter?
Fixed.
msg196887 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2013-09-04 04:01
New changeset 96e55a1a0de7 by Andrew Svetlov in branch 'default':
Issue #18882: Add threading.main_thread() function.
http://hg.python.org/cpython/rev/96e55a1a0de7
msg196894 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2013-09-04 06:26
I don't think you saw my review, but could you add a docstring to the main_thread() function? Thanks!
msg196895 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2013-09-04 07:36
I did not received review email, sorry.
Docstring is added.
BTW 'threading' module has almost no docstrings, that's why I've added only docs at first.
Do you think docstrings should be added to all public functions?
Thanks.
msg196896 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2013-09-04 08:24
> BTW 'threading' module has almost no docstrings, that's why I've
> added only docs at first.
> Do you think docstrings should be added to all public functions?

Well, probably, although that's another issue :)
msg212053 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-02-24 02:19
In msg196529 Antoine says there are two possible interpretations of main_thread, and we must choose one.  However, the documentation does not indicate which one was chosen.
msg212905 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2014-03-07 22:21
Implementation uses the first choice:
main_thread() returns the original _MainThread instance, even if it's dead in the child process.

I'm sorry, would you guess desired documentation change?
History
Date User Action Args
2022-04-11 14:57:50adminsetgithub: 63082
2014-03-07 22:21:04asvetlovsetmessages: + msg212905
2014-02-24 02:19:48r.david.murraysetnosy: + r.david.murray
messages: + msg212053
2013-09-04 08:24:44pitrousetmessages: + msg196896
2013-09-04 07:36:02asvetlovsetmessages: + msg196895
2013-09-04 06:26:23pitrousetmessages: + msg196894
2013-09-04 04:08:19asvetlovsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2013-09-04 04:01:25python-devsetnosy: + python-dev
messages: + msg196887
2013-09-01 05:20:42asvetlovsetfiles: + issue18882-4.diff

messages: + msg196707
2013-08-31 17:53:16pitrousetmessages: + msg196658
2013-08-31 16:37:26serhiy.storchakasetnosy: + serhiy.storchaka

type: enhancement
components: + Library (Lib)
stage: patch review
2013-08-31 03:49:16asvetlovsetfiles: + issue18882-2.diff

messages: + msg196615
2013-08-30 13:56:03pitrousetdependencies: - Allow registering at-fork handlers
messages: + msg196540
2013-08-30 13:30:05asvetlovsetdependencies: + Allow registering at-fork handlers
messages: + msg196535
2013-08-30 12:49:15asvetlovsetmessages: + msg196531
2013-08-30 12:48:17asvetlovsetfiles: + issue18882.diff
keywords: + patch
messages: + msg196530
2013-08-30 12:47:11pitrousetmessages: + msg196529
2013-08-30 12:45:07giampaolo.rodolasetnosy: + giampaolo.rodola
2013-08-30 12:29:27christian.heimessetmessages: + msg196526
2013-08-30 12:18:37christian.heimessetnosy: + christian.heimes
2013-08-30 10:26:46asvetlovcreate