线程中的join()函数 [英] The join() function in threading

查看:127
本文介绍了线程中的join()函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我最近试图理解join()函数,但是我阅读的许多教程/文档似乎都不了解.这里也许有人可以向我解释吗?

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?

推荐答案

join多线程方法

join method for multithreading

关于将join用于线程和多线程,将使用t.join().很简单,为您完成的所有操作就是您请求加入一个线程,该线程等待线程终止其运行.您的解释器将一直运行,直到所有线程终止.对于长时间运行的线程,请创建一个守护进程!也就是说,通过执行以下操作使线程守护程序完成:

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()

daemon=True部分是该代码显示给线程的唯一重要部分,但是如有必要,我可以对其进行扩展.

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字符串方法

join method for strings

要了解join()函数,请对Python的创建者命名为join()进行直观思考,以创建此命令...他们想创建一个可以加入东西"的命令并执行此操作它灵活.想象一下,东西"是一堆字符串.现在假设您想将它们放在一起,并以计算快速的方式(稍后再介绍),假设您有很多这样的字符串,该怎么办?您可以使用join()以所需的灵活方式组合字符串.通过灵活的方式,您可以将字符串以空格,逗号或完全没有空格等分隔的方式组合在一起.例如,考虑以下不同字符串的列表:

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'] 

现在,让我们看一些使用join()灵活加入列表的示例(几乎我们喜欢的任何方式):

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

','.join(my_list)

产生输出:

'I,love,join,so,much'

或者让我们尝试一下:

''.join(my_list)

产生输出:

'Ilovejoinsomuch'

或者让我们尝试以下示例:

Or let's try this example:

' '.join(my_list)

产生输出:

'I love join so much'

请注意在所有这些情况下,如何将字符串列表连接在一起成为一个字符串,并注意如何精确指定希望如何进行连接.现在join()背后的真正力量在于,您希望加入的对象可以是各种不同的类型:字典,列表,元组,生成器等.不是非Pythonic的;)可以在其上实现join()所有这些不同的实体一次?这是Python真正脱颖而出的地方,它使您能够坐下来并说出您希望如何将对象结合在一起的同时,将所有这些不同类型的结构结合在一起.这就是您的灵活性所在.

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)

这将输出以下输出:

'I 20 same'

当然,您可以自由使用其他方法将字符串连接在一起.如果只需要连接几个字符串,并且不需要像join()这样的计算快速方式,则可以使用+号.例如:

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))

产生输出:

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

但是,如果您有很多这样的字符串文字,则最好使用join()进行组合,以免出现内存和垃圾回收之类的问题.

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)) 

您的输出将是<type 'str'>,您将知道!

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

希望这会有所帮助!

这篇关于线程中的join()函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆