threads, processes, co-routines

Source: Internet
Author: User

Thread

There are two ways to create a thread from the threading module, as follows:

Mode 1:

# !/usr/bin/env python # Coding=utf-8 Import Threading def F1 (ARG):     Print (ARG) if __name__ ' __main__ ' :     = Threading. Thread (TARGET=F1, args= (1,))    t1.start ()#  output #  1 

Method 2:

class MyThread (Threading. Thread):    def__init__(self, Func, args):        = func        = args        Super (MyThread, self). __init__ ()    def  Run (self):        self.func (Self.args)def  F2 (ARG):     Print  = MyThread (F2, 123) Obj.start ()

Output
# 123

Setdaemon method

The main thread is not equal to the end of the thread execution.

#!/usr/bin/env python#Coding=utf-8ImportThreadingImport TimedefF1 (ARG): Time.sleep (2)    Print('Start', Arg)if __name__=='__main__': T1= Threading. Thread (TARGET=F1, args= (1,)) T1.setdaemon (True) T1.start ()Print(123)
Output
# 123 instead of 1 12

Join method

Indicates that the main thread waits for the child thread to finish executing, and the join parameter represents the maximum number of seconds

#!/usr/bin/env python#Coding=utf-8ImportThreadingImport TimedefF1 (ARG): Time.sleep (2)    Print('Start', Arg)if __name__=='__main__': T1= Threading. Thread (TARGET=F1, args= (1,)) T1.setdaemon (True) T1.start () T1.join ()Print(123)
Output
# 1 123

threads, processes, co-routines

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.