0

So I recently tried to understand the join() function, but as many tutorials/documentations I read I just don't seem to understand it. Is there maybe someone here who is capable of explaining it to me?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Tsumugi Kotobuki
  • 103
  • 1
  • 1
  • 10
  • `t.join()` will block until the `t` object has finished it's `run` method. Can you be more specific about what you don't understand? – dano Aug 17 '14 at 01:04
  • @dano what will be blocked? Any other thread? – Tsumugi Kotobuki Aug 17 '14 at 01:06
  • Please read this, http://stackoverflow.com/questions/15085348/what-is-the-use-of-join-in-python-threading – James Sapam Aug 17 '14 at 01:08
  • No, the thread that executes the `join`. That is, some thread, which has a reference to another thread in a variable called `t`, calls `join()` on that reference. That call causes the calling thread to block until the referenced thread (`t`) finishes running. – Mark Reed Aug 17 '14 at 03:25

1 Answers1

3

join method for multithreading

Regarding the use of join for threads and multithreading, you would use t.join(). Very simply, all this does for you is you request to join with a thread, which waits for the thread to terminate its run. Your intepreter will stay running until all the threads terminate. For threads that run a long time, make a daemon! That is, make the thread daemonic by doing, for instance:

t= Thread(target=entry, args=(5,), daemon=True)
t.start()

The daemon=True part is the only important part of that code to show for the thread, but I can expand it if necessary.

This would be great for handling any sort of background tasks that you have running. When the main thread terminates, daemonic threads are automatically eliminated.

join method for strings

To understand the join() function, think intuitively about what the creators of Python set out to do with this command by naming it join()... they wanted to create a command that could join "stuff" and to do it flexibly. Imagine that "stuff" is a bunch of strings. Now imagine that you want to put them all together, and do so in a computationally fast manner (more on this later) assuming you have lots of these strings, what do you do? You use join() to combine the strings in the flexible manner that you wish. By flexible, you could combine the strings together in a way that they are separated by spaces, or commas, or no white space at all, etc. For example, consider this list of different strings:

my_list = ['I', 'love', 'join', 'so', 'much'] 

Now let's see some examples of using join() to flexibly join the list (in pretty much any way we like):

','.join(my_list)

Produces output:

'I,love,join,so,much'

Or let's try this one:

''.join(my_list)

Produces output:

'Ilovejoinsomuch'

Or let's try this example:

' '.join(my_list)

Produces output:

'I love join so much'

Notice how in all these cases, your list of strings gets joined together into one string and notice how you can specify exactly how you want the joining to occur. Now the real power behind join() is that the objects you wish to join could be all sorts of different types: dictionaries, lists, tuples, generators, etc. Wouldn't it be non-Pythonic ;) to implement join() on all these different entities one at a time? This is where Python really stands out in terms of being able to join all these different kinds of structures together while you just sit back and tell it how you want the objects to be joined together. This is where your flexibility comes in.

Now I want to show you a very cool example of being able to combine together a bunch of different object types using a generator expression:

my_list = ['I', 20, 'same']
print ' '.join(str(x) for x in my_list)

This prints the following output:

'I 20 same'

Of course, you are free to use other methods to join strings together. If you only have a couple of strings to join, and you have no need to do it in a computationally fast manner like join(), you can just use the + sign. For example:

string1 = 'my string'
string2 = 'your string'

print(string1 + ' ' + string2)
print('{} {}'.format(string1,string2))

Produces output:

'my string your string'
'my string your string'

But if you have lots of these string literals you are better of using join() to combine them so that you don't run into issues with memory and garbage collection and the like.

By the way, if you are ever wondering what kind of output you get (is it a string, or what in the world is it?), just type for example:

print type('{} {}'.format(string1,string2)) 

Your output will be <type 'str'> and you will know!

Hope this helps!

warship
  • 2,924
  • 6
  • 39
  • 65
  • 2
    `str.join` has nothing to do with the `join` method of threads. – roippi Aug 17 '14 at 02:35
  • @XYZ927 it would be nice if you would maybe have an example of the join function in use for multithreading. – Tsumugi Kotobuki Aug 17 '14 at 03:13
  • 1
    *"However, you cannot join daemonic threads"*. That's not true. You can `join` daemonic threads. Daemonic threads just aren't *automatically* joined when the main thread exits. Also, this: `daemon=TRUE` should be: `daemon=True`. – dano Aug 17 '14 at 03:21
  • 1
    @dano +1. Fixed, thank you. Thank you for the downvote. Next time you may also consider editing my response. – warship Aug 17 '14 at 03:23
  • 1
    @TsumugiKotobuki There is a really great resource I found here showing examples of join function for multithreading: http://pymotw.com/2/threading/ . Please let me know if you have any questions. – warship Aug 17 '14 at 03:35