Python Threading Learning

Source: Internet
Author: User

Thread Lock

#!/usr/bin/python

Import threading

Import time

Class MyThread (threading. Thread):

def __init__ (self,threadname):

Threading. THREAD.__INIT__ (self,name=threadname) Set thread name

def run (self):

Global X

Lock.acquire () plus lock

For I in range (3):

X=x+1

Time.sleep (2)

Print X

Lock.release () Release lock, allowing the next thread to start execution

Lock=threading. Rlock () generates Rlock object for lock

T1=[]

For I in range (10):

T=mythread (str (i)) generates 10 threads

T1.append (t)

X=0

For i in T1:

I.start () 10 threads start executing and execute one next before you can start execution


This article is from the "Expect batch Sync data" blog, so be sure to keep this source http://4249964.blog.51cto.com/4239964/1573082

Python Threading Learning

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.