Use threading.Thread modular , There are also two ways to use it , Available classes , You can also pass in a function or class instance in an instantiated object .

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from threading import Thread
import time
def run_thread(n):
for i in range(n):
print i
class race(Thread):
def __init__(self,threadname,interval):
Thread.__init__(self,name=threadname)
self.interval = interval
self.isrunning = True
def run(self):
while self.isrunning:
print 'thread %s is running,time:%s\n' %(self.getName(),time.ctime())
time.sleep(self.interval)
def stop(self):
self.isrunning = False
def test():
t1 = Thread(target=run_thread,args=(5,))
t1.start()
thread1 = race('A',1)
thread2 = race('B',2)
thread1.start()
thread2.start()
time.sleep(4)
thread1.stop()
thread2.stop()
if __name__ == '__main__':
test()

PYTHON Learn thread knowledge again B More articles about

  1. PYTHON Learn thread knowledge again A

    I saw it a while ago LINUX The thread of , Sync , After information numbers and things like that , Again in understanding PYTHON Threads feel different again . Do some tests . thread: Module provides basic thread and lock support threading: Provides a higher level , work ...

  2. PYTHON Learn thread knowledge again F--- Queue synchronization Queue

    Let's consider a more complex scenario : The products are different . It is not enough to record only one quantity , You also need to record the details of each product . It's easy to think of the need for a container to record these products . Python Of Queue Synchronous... Is provided in the module . Thread safe ...

  3. PYTHON Learn thread knowledge again E--- Conditional variable synchronization Condition

    Python Provided Condition Object provides support for complex thread synchronization problems .Condition It's called a conditional variable , In addition to providing with Lock Allied acquire and release Outside method , It also provides wait and notify ...

  4. PYTHON Learn thread knowledge again C--- Thread mutex

    Combine examples , That makes sense . Just don't let the shared variables be executed out of order by each thread , The result is unexpected threading Defined in the module Lock class , Easy to handle locking : # Create a lock mutex = threading.Lock( ...

  5. PYTHON Learn thread knowledge again G-- Inter thread communication Event

    A lot of times , Threads need to communicate with each other . A common situation is that the secondary thread performs specific tasks for the primary thread , In the process of implementation, we need to report the progress of implementation . The previous conditional variable synchronization has involved the communication between threads (threading.Condit ...

  6. PYTHON Learn thread knowledge again D--- Reentrant lock

    Not much to explain , Prevent the normal lock irregular acquisition and release #!/usr/bin/env python # -*- coding: utf-8 -*- import threading import time class ...

  7. Thread concept ( Thread features , The relationship between process and thread , Threads and python Theoretical knowledge , Thread creation )

    Reference blog : https://www.cnblogs.com/xiao987334176/p/9041318.html The introduction background of thread concept process We've seen the concept of processes in the operating system before , Programs don't run alone ...

  8. python The whole development of the stack ,Day41( Thread concept , Thread features , The relationship between process and thread , Threads and python Theoretical knowledge , Thread creation )

    Yesterday's review queue queue : fifo . Data process security Queue implementation : The Conduit + lock Producer consumer model : Solve the imbalance between data supply and demand The Conduit Two-way communication The data process is not secure EOFError: Pipes are run by the operating system ...

  9. python The whole development of the stack ,Day41( Thread concept , Thread features , The relationship between process and thread , Threads and python Theoretical knowledge , Thread creation )

    Yesterday's review queue queue : fifo . Data process security Queue implementation : The Conduit + lock Producer consumer model : Solve the imbalance between data supply and demand The Conduit Two-way communication , The data process is not secure EOFError: Pipes are referenced by the operating system must ...

Random recommendation

  1. VMware Workstation 12 Serial number

    VMware Workstation 12 Serial number :5A02H-AU243-TZJ49-GTC7K-3C61N Just like before Microsoft , Let's use piracy as well , So you can have market share faster . in fact , Input key It's permanent ...

  2. Use Nominatim Conduct openstreetmap Address search / analysis

    Nominatim( From Latin , intend “ name ”) It's a search by name and address OSM Data in , And generate OSM Point synthesis address tool ( Reverse geocoding ). Available in http://nominatim.openstreet ...

  3. SpringMVC Learning notes ( Two )

    One .HandleMapping and HandlerAdapter Explanation HandleMapping: Process mapper , It can be understood as requested url Find the corresponding Controller class . HandlerAdapter: can ...

  4. shell obtain ip

    ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"

  5. utilize RNAseq Cluster analysis of data

    library(ConsensusClusterPlus)library(factoextra)library(cluster)library(NbClust)# Read in the data data = read.ta ...

  6. Webshell Research on realization and concealment

    One . What is? webshell webshell brief introduction webshell, seeing the name of a thing one thinks of its function :web Refer to web Server , and shell It's a script program written in script language ,webshell Is is web A management of Tools , Sure ...

  7. application TcpListener Realized socket Server side

    Preface In the project, a simple socket Server side , Adopted TcpListener This class . In addition to the basic functions , There are several points that need attention . To be able to receive multiple client connections at the same time , Of course , You don't need thousands of them . To be able to detect customers ...

  8. ZOJ 3879 Capture the Flag

    In memory of my first article acm Blog , The first simulation :) http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3879 The meaning is very complicated , Feeling ...

  9. C# List of network resources

    1, http://club.topsage.com/thread-371996-1-1.html

  10. laravel Observer mode

    laravel Observer mode : event