Graceful start multithreading [Python]

Source: Internet
Author: User

Python in multi-threading, if write a call iamthreading inherited multi-threaded class, how to start it, is a problem.

What's the problem? It's not starting.

For I in Xrange (threads_num):  t = iamthreading ()  threads.append (t) for I in Xrange (threads_num):  Threads[i] . Start () for I in Xrange (threads_num):  threads[i].join ()


But isn't it too ugly? After a long use, unbearable, too ugly!

So come a dashing

While 1:  iamthreading (). Start ()
One go, however, this can not stop the program to run, can't stop, not satisfied.

So, I wrote:

               threads = [Iamthreadingr () for I in Xrange (threads_num)]        if [(J.setdaemon (True), J.start ())-j in threads] = = [ I.join () for I in Threads]: print ' All wars is civil wars,because all men is a                . '

Take this to get rid of ugly for parallelism sentence, but see "= =" equal sign, will not just calculate a discovery on the right side, go to the next step?

See if Python will do this:

>>> from Queue import queue>>> q = Queue (0) >>> q.put (9) >>> Q.put (8) >>> Q.put (7) >>> Q.put (6) >>> if [1, 2, 3] = = [Q.get () for I in Xrange (3)]: ...   Print "Hei hei" ...>>> q.get () 6>>>
It seems that Python will produce a complete list to compare.

Try again:

>>> from Queue import queue>>> q = Queue (0) >>> q.put (9) >>> Q.put (8) >>> Q.put (7) >>> Q.put (6) >>> if [] = = [Q.get () for I in Xrange (3)]: ...   Print "Hei hei" ...>>> q.get () 6>>>
This is reassuring, it seems that the method is good.




Graceful start multithreading [Python]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.