Skip to content
This repository has been archived by the owner on Apr 22, 2020. It is now read-only.

concurrency: Add parent's name to new thread's name #197

Merged
merged 1 commit into from
Aug 11, 2019

Conversation

YuvalEvron
Copy link
Contributor

Fixes #174

@idanarye
Copy link

idanarye commented Aug 8, 2019

This can easily explode:

In [1]: import threading

In [2]: from easypy.concurrency import concurrent, MultiObject

In [3]: def recurse(depth):
   ...:     print('%s remain, I am %r' % (depth, threading.current_thread().name))
   ...:     if 0 < depth:
   ...:         with concurrent(recurse, depth - 1):
   ...:             pass
   ...:

In [4]: recurse(3)
3 remain, I am 'MainThread'
2 remain, I am 'MainThread::7F867069D5F8'
1 remain, I am 'MainThread::7F867069D5F8::7F867069DEF0'
0 remain, I am 'MainThread::7F867069D5F8::7F867069DEF0::7F8670842208'

We usually want only the first (context) and last (actual thread to filter on) parts.

@idanarye
Copy link

idanarye commented Aug 8, 2019

Also, this doesn't work for MultiObject:

In [6]: MultiObject(range(5)).call(lambda n: print(n, threading.current_thread().name))
0 ThreadPoolExecutor-1_0
1 ThreadPoolExecutor-1_0
2 ThreadPoolExecutor-1_0
3 ThreadPoolExecutor-1_1
4 ThreadPoolExecutor-1_0
Out[6]: <MultiObject[NoneType] (x5/None)>

@YuvalEvron
Copy link
Contributor Author

Also, this doesn't work for MultiObject:

In [6]: MultiObject(range(5)).call(lambda n: print(n, threading.current_thread().name))
0 ThreadPoolExecutor-1_0
1 ThreadPoolExecutor-1_0
2 ThreadPoolExecutor-1_0
3 ThreadPoolExecutor-1_1
4 ThreadPoolExecutor-1_0
Out[6]: <MultiObject[NoneType] (x5/None)>

This looks like a problem on MultiObject's side. We need to find out why there is not thread name, but this should be a different PR.

@YuvalEvron YuvalEvron merged commit d039dac into master Aug 11, 2019
@YuvalEvron YuvalEvron deleted the meaningful-threads-names branch August 11, 2019 09:42
@koreno
Copy link
Contributor

koreno commented Aug 11, 2019

Also, this doesn't work for MultiObject:

In [6]: MultiObject(range(5)).call(lambda n: print(n, threading.current_thread().name))
0 ThreadPoolExecutor-1_0
1 ThreadPoolExecutor-1_0
2 ThreadPoolExecutor-1_0
3 ThreadPoolExecutor-1_1
4 ThreadPoolExecutor-1_0
Out[6]: <MultiObject[NoneType] (x5/None)>

This looks like a problem on MultiObject's side. We need to find out why there is not thread name, but this should be a different PR.

MultiObject uses python's thread-pool framework, which controls the name. We could do that as long as we don't need to monkey-patch anything - I don't want to deal with more breakages due to python version differences.

self.threadname = kwargs.pop('threadname', None)
if not self.threadname:
current_thread_name = threading.current_thread().name
if current_thread_name:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a thread that doesn't have a name?
I think every concurrent call is going to have a MainThread:: prefix...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't remember seeing something like this, but wanted to be on the safe side.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well I think the MainThread:: prefix is redundant and shouldn't be there. A concurrent thread spawned by the main thread should just have its anon-xxx name with no prefix.
would you fix that for us?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Based unnamed thread names on named threads
3 participants