[英]How to handle error in threading when doing multiple schedule functions in python?
Hi I have multiple functions running with thread.您好,我有多个函数与线程一起运行。 I am wondering how do you handle error in running multiple threads where it does not affect the other threads that are running.
我想知道在不影响正在运行的其他线程的情况下,您如何处理运行多个线程的错误。 When a function breaks, the other functions stops too.
当 function 中断时,其他功能也会停止。 Do I use a try except for every schedule?
除了每个时间表,我是否使用尝试? Or is there a better way?
或者,还有更好的方法?
Current structure is like this.目前的结构是这样的。
#--------------------------------------------------------------------------run_threaded function
def run_threaded(job_func):
job_thread = threading.Thread(target=job_func)
job_thread.start()
#------------------------------------------------------------------------scheduler function
#scheduler
print('scheduler is starting')
schedule.every().day.at('06:30').do(run_threaded,lambda:functionA())
schedule.every().day.at('06:30').do(run_threaded,lambda:functionB())
schedule.every().day.at('06:30').do(run_threaded,lambda:functionC())
schedule.every().day.at('06:30').do(run_threaded,lambda:functionD())
schedule.every().day.at('06:30').do(run_threaded,lambda:fucntionE())
schedule.every().day.at('22:00').do(lambda:email())
schedule.every().day.at('22:15').do(lambda:writeFile())
while True:
schedule.run_pending()
sleep(1)
I want to start by stating that[Python.Docs]: threading - Thread-based parallelism does not actually do multithreading (on multiple CPU s), but it's more like a (lame) workaround ( gainarie ), because of GIL .我想首先声明[Python.Docs]: threading - Thread-based parallelism实际上并不执行多线程(在多个CPU上),但它更像是一个(蹩脚的)解决方法( gainarie ),因为GIL 。
If you really want parallel execution, use [Python.Docs]: multiprocessing - Process-based parallelism .如果您真的想要并行执行,请使用[Python.Docs]: multiprocessing - Process-based parallelism 。
But even if the above wasn't true, you actually do not have multithreading in your code.但即使上述情况并非如此,您的代码中实际上也没有多线程。 According to [ReadTheDocs.Schedule]: Schedule (if this is the module you are using), the methods (like do ) expect a function as a parameter (same as threading.Thread initializer), but you're passing a function call (the parentheses () at the end).
根据[ReadTheDocs.Schedule]: Schedule (如果这是您正在使用的模块),方法(如do )期望 function 作为参数(与threading.Thread初始值设定项相同),但您传递的是 function 调用(最后的括号() )。
So, you have sequential runs of the functions you're passing, and when the 1 st one raises an exception, the whole program stops.因此,您可以按顺序运行所传递的函数,并且当第一个函数引发异常时,整个程序将停止。
Taking the below line as an example:以下面一行为例:
schedule.every().day.at('06:30').do(run_threaded, lambda: functionA())
In [ReadTheDocs.Schedule]: Parallel execution (which was probably one of the sources of inspiration) the (target) function isn't called , so the above line should be:在[ReadTheDocs.Schedule]: Parallel execution (这可能是灵感的来源之一) the (target) function isn't called ,所以上面的行应该是:
schedule.every().day.at('06:30').do(run_threaded, lambda: functionA) # functionA isn't called (no parentheses at the end) but it's rather passed as an argument
Not sure what's lambda purpose, things would look much simpler without it:不确定lambda的目的是什么,如果没有它,事情看起来会简单得多:
schedule.every().day.at('06:30').do(run_threaded, functionA)
Apply the above steps to all the calls, and I think you'd be fine (functions runs that raise exceptions won't affect the ones that don't - at least that's the behavior in threading ), in spite of the following (in the Schedule documentation URL ):将上述步骤应用于所有调用,我认为你会很好(引发异常的函数运行不会影响那些不引发异常的函数 - 至少这是threading中的行为),尽管有以下(在附表文档URL ):
When not to use Schedule
何时不使用时间表
...
...
- Concurrent execution (multiple threads)
并发执行(多线程)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.