homepage

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.

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

AltStyle によって変換されたページ (->オリジナル) /