PYTHON Multi-threaded semaphore

Source: Internet
Author: User

#!/usr/bin/env python# -*- coding: utf-8 -*-import threadingimport  Timeimport randomdef work_func ():    print  "Worker thread is  started at %s "% threading.current_thread ()     random.seed ()      time.sleep (Random.random ())     print  "Worker thread is  finished at %s "%threading.current_thread () Def sinple_thread_demo ():     for i in range (+):         t=threading. Thread (Target=work_func)         t.start () Def worker_func_lock ( Lock):     lock.acquire ()     work_func ()      Lock.release () glock=threading. Lock () Gsem=threading._semaphore () #信号量, number of threads allowed to run concurrently Def thread_demo_lock ():     for  i in range (:         #sl =threading. Thread (Target=worker_func_lock,args=[glock])         sl=threading. Thread (Target=worker_func_lock,args=[gsem]) #参数传入信号量         sl.start () if __name__== "__main__":     #sinple_thread_demo () #通过锁实现串行      Thread_demo_lock () #实现同时运行多个线程工作, mainly through the semaphore settings, but still executed on a CPU, the specific implementation of the example can be implemented in the function, to achieve single-core multi-concurrency


This article is from the "DBSpace" blog, so be sure to keep this source http://dbspace.blog.51cto.com/6873717/1882455

PYTHON Multi-threaded semaphore

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.