后台工作者异常处理(Background worker exception handling)
我对如何处理异常有些困惑。
我有一个后台工作线程运行一些长时间运行的进程。 我的理解是,如果后台工作线程发生异常,代码仍然会以RunWorkerCompleted方法结束。
void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { if (e.Error != null) throw e.Error;
如果是这种情况,那么围绕bgWorker.RunWorkerAsync()放置一个try catch块就有任何意义。 打电话,我假设不是?
我想重新抛出在RunWorkerCompleted方法中捕获的异常,我如何在不丢失堆栈跟踪的情况下做到这一点 - 我上面正确的是什么? 我在重新抛出一个异常时读到了你,你应该使用“throw”?
I am slightly confused on how to deal with an exception.
I have a background worker thread that runs some long running process. My understanding is if an exception occurs on the background worker thread the code will still end up at the RunWorkerCompleted method.
void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { if (e.Error != null) throw e.Error;
If this is the case is there any point in putting a try catch block around the bgWorker.RunWorkerAsync(); call, I assume not?
I want to rethrow the exception that is caught in the RunWorkerCompleted method, how can I do this without losing the stack trace - is what I have above correct? I read that you when rethrowing an exception you should just use "throw"?
原文:https://stackoverflow.com/questions/20582253
最满意答案
Django在内部使用UTC时间。
TIME_ZONE
将被用于“您的观点和模型”( https://docs.djangoproject.com/en/1.9/ref/settings/#std:setting-TIME_ZONE )你从
2016-01-11 00:00 CET
开始,即2016-01-10 23:00 UTC
! 您的日期时间已正确保存到数据库并在以后进行了恢复,因此所有内容都按预期工作。Django uses UTC time internally.
TIME_ZONE
will be used "for your views and models" (https://docs.djangoproject.com/en/1.9/ref/settings/#std:setting-TIME_ZONE)You started with
2016-01-11 00:00 CET
, which is2016-01-10 23:00 UTC
! Your datetime was correctly saved to the database and later restored, so everything is working as expected.
相关问答
更多-
您似乎错过了将天真日期时间对象分配回value datetime对象。 它应该是 value = value.replace(tzinfo=None) # assign 'naive' datetime object back to 'value' 而不仅仅是 value.replace(tzinfo=None) # will just return 'naive' datetime object 代码现在变成: import pytz import datetime .... for i, row i ...
-
Django在内部使用UTC时间。 TIME_ZONE将被用于“您的观点和模型”( https://docs.djangoproject.com/en/1.9/ref/settings/#std:setting-TIME_ZONE ) 你从2016-01-11 00:00 CET开始,即2016-01-10 23:00 UTC ! 您的日期时间已正确保存到数据库并在以后进行了恢复,因此所有内容都按预期工作。 Django uses UTC time internally. TIME_ZONE will be ...
-
Django DateField auto_now_add提供了auto_now_add和auto_now参数: date = models.DateField(auto_now=True, blank=True, null=True) date_added = models.DateTimeField(auto_now_add=True, blank=True, null=True) I've figured out, that the problem is with django-admin. If ...
-
改变这一行: self.ends.replace(tzinfo=timezone.utc) 至 timezone.make_aware(self.ends, timezone.get_current_timezone()) 解决了这个问题! Changing this line: self.ends.replace(tzinfo=timezone.utc) to timezone.make_aware(self.ends, timezone.get_current_timezone()) Solve ...
-
使用date()方法: datetime.datetime.now().date() datesrt=models.DateTimeField(default=datetime.datetime.now().date(),blank=True) Use the date() method: datetime.datetime.now().date() datesrt=models.DateTimeField(default=datetime.datetime.now().date(),blank=Tr ...
-
数据库的结果取整为秒,因此15:49.21.93894 + 1.00变为15:49:21 + 02:00 这是完全错误的。 在将datetime对象转换为字符串now.strftime('%m.%d.%Y %H:%M:%S')您已经放弃了now.strftime('%m.%d.%Y %H:%M:%S')微秒的部分。 而且,+02 +02:00是日期时间对象的时区utc偏移量。 这不是一个圆整的秒或什么。 真正的问题在于你的strftime字符串格式。 今天,你的时区可能有夏令时。 但是,当您将错误的格式化日 ...
-
Django,DateTimeField(auto_now_add = True)不起作用(Django, DateTimeField(auto_now_add=True) not working)[2022-08-14]
如错误所示,您无法设置auto_now_add=True并同时指定默认值。 问题是Django需要知道数据库中现有条目的值。 您可以设置null=True ,然后该值将保留为None 。 created_time = models.DateTimeField('Created Time', auto_now_add=True, null=True) 或者,只需删除默认值,然后再次运行makemigrations。 created_time = models.DateTimeField('Created T ... -
Django引用 https://docs.djangoproject.com/en/1.9/ref/settings/#datetime-input-formats 答案如下: https://stackoverflow.com/a/10855090/1990671 Django references to https://docs.djangoproject.com/en/1.9/ref/settings/#datetime-input-formats Here's the answer: https: ...
-
DateTimeField的文档准确显示了它接受的格式以及如何自定义。 如果未提供input_formats参数,则默认输入格式为: ['%Y-%m-%d %H:%M:%S', # '2006-10-25 14:30:59' '%Y-%m-%d %H:%M', # '2006-10-25 14:30' '%Y-%m-%d', # '2006-10-25' '%m/%d/%Y %H:%M:%S', # '10/25/2006 14:30:59' '%m/ ...
-
使用Django 1.6和PostGreSQL 9.2存储时区感知日期时间(Storing timezone aware datetimes with Django 1.6 and PostGreSQL 9.2)[2022-11-15]
来自文档 当启用对时区的支持时,Django将日期时间信息以UTC格式存储在数据库中,在内部使用时区感知日期时间对象,并将它们转换为模板和表单中最终用户的时区。 因此,当您在数据库中节省时间时,Django会自动将其存储在UTC中。 当您在模板或表单中渲染时,Django会根据您的设置自动将其转换为用户的timeozone或默认时区( TIME_ZONE = 'Europe/Paris' )。 这对Django 1.4及以上版本有效。 回答你的问题: Django将日期时间转换为UTC,但不确定它是否是一件 ...