jjzjj

Threading

全部标签

python - 如何限制 python 中的事件线程数?

我是python的新手,在threading方面取得了一些进展-我正在做一些音乐文件转换,希望能够利用我机器上的多个内核(每个内核一个事件转换线程)。classEncodeThread(threading.Thread):#thisishackedtogetherabit,butshouldgiveyouanideadefrun(self):decode=subprocess.Popen(["flac","--decode","--stdout",self.src],stdout=subprocess.PIPE)encode=subprocess.Popen(["lame","--qu

python - 非轮询/非阻塞计时器?

到目前为止,我发现的最佳解决方案是只使用sleep()函数。我想在定时器到期事件发生时运行我自己的回调函数。是否有任何事件驱动的方式来解决这个问题?fromtimeimportsleep#Sleepforaminutetime.sleep(60) 最佳答案 有一个内置的简单解决方案,使用threading模块:importthreadingtimer=threading.Timer(60.0,callback)timer.start()#after60seconds,'callback'willbecalled##(inthemea

python - 如何启动和停止线程

我如何使用我糟糕的线程类启动和停止线程?它在循环中,我想在代码的开头重新启动它。我该如何启动-停止-重启-停止-重启?我的类(class):importthreadingclassConcur(threading.Thread):def__init__(self):self.stopped=Falsethreading.Thread.__init__(self)defrun(self):i=0whilenotself.stopped:time.sleep(1)i=i+1在主代码中,我想要:inst=Concur()whileconditon:inst.start()#Aftersome

python - 我如何排队我的 Python 锁?

有没有办法让python锁排队?到目前为止,我一直在我的代码中假设threading.lock在队列上运行。看起来它只是把锁给了一个随机的储物柜。这对我来说很糟糕,因为我正在工作的程序(游戏)高度依赖于以正确的顺序获取消息。python中有排队锁吗?如果是这样,我会损失多少处理时间? 最佳答案 我完全同意评论声称您可能正在以一种没有结果的方式思考这个问题。锁提供序列化,根本不旨在提供排序。执行订单的标准、简单且可靠的方法是使用Queue.QueueCPython让操作系统决定获取锁的顺序。在大多数系统上,这看起来或多或少是“随机的”

Python中threading模块 lock、Rlock的使用

一、概述在使用多线程的应用下,如何保证线程安全,以及线程之间的同步,或者访问共享变量等问题是十分棘手的问题,也是使用多线程下面临的问题,如果处理不好,会带来较严重的后果,使用python多线程中提供Lock、Rlock、Semaphore、Event、Condition用来保证线程之间的同步,后者保证访问共享变量的互斥问题。Lock&RLock:互斥锁,用来保证多线程访问共享变量的问题Semaphore对象:Lock互斥锁的加强版,可以被多个线程同时拥有,而Lock只能被某一个线程同时拥有。Event对象:它是线程间通信的方式,相当于信号,一个线程可以给另外一个线程发送信号后让其执行操作。Co

Python - threading.Timer 在调用 cancel() 方法后保持事件状态

我注意到以下代码中的以下行为(使用threading.Timer类):importthreadingdefontimer():printthreading.current_thread()defmain():timer=threading.Timer(2,ontimer)timer.start()printthreading.current_thread()timer.cancel()iftimer.isAlive():print"Timerisstillalive"iftimer.finished:print"Timerisfinished"if__name__=="__main__

Python线程模块导入失败

我正在尝试导入线程模块,但是,我似乎无缘无故地收到错误。这是我的代码:importthreadingclassTheThread(threading.Thread):defrun(self):print'Insertsomethreadstuffhere.'print'I\'llbeexecuted...yeah....'print'There\'snotmuchtoit.'TheThread.Start()错误:Traceback(mostrecentcalllast):File"threading.py",line1,inimportthreadingFile"C:\Users\T

Python线程模块导入失败

我正在尝试导入线程模块,但是,我似乎无缘无故地收到错误。这是我的代码:importthreadingclassTheThread(threading.Thread):defrun(self):print'Insertsomethreadstuffhere.'print'I\'llbeexecuted...yeah....'print'There\'snotmuchtoit.'TheThread.Start()错误:Traceback(mostrecentcalllast):File"threading.py",line1,inimportthreadingFile"C:\Users\T

Python的threading模块

 为引入多线程的概念,下面是一个例子:importtime,datetimestartTime=datetime.datetime(2024,1,1,0,0,0)whiledatetime.datetime.now()在等待time.sleep()的循环调用完成时,程序不能做任何事情,它只是在那里做着,直到2029年万圣节。这是因为Python程序在默认情况下,只有一个执行线程。执行线程在下载文件时,在设置了一次只能下载一个文件的程序中,同一时间段的下载任务中只能执行下载一个文件,这就是单线程。示例图如下: 在设置同时可下载2个及以上文件的程序中,同一时间段的下载任务可以同时执行下载多个文件,

c# - System.Timers.Timer 与 System.Threading.Timer 的线程安全

在本文中:http://msdn.microsoft.com/en-us/magazine/cc164015.aspx作者声明System.Threading.Timer不是线程安全的。从那时起,这在博客上、Richter的书“CLRviaC#”和SO中都重复了这一点,但这从来没有被证明是合理的。此外MSDNdocumentation确保“这种类型是线程安全的。”1)谁说的是真话?2)如果这是原始文章,是什么让System.Threading.Timer不是线程安全的,以及它的包装器System.Timers.Timer如何实现更多的线程安全?谢谢 最佳答案