线程安全相当于python的time.strptime()?
当我在线程中使用 time.strptime() 时,我写的东西会抛出很多 AttributeError 异常。这似乎只发生在 Windows 上(而不是 Linux 上),但无论如何 - 通过谷歌搜索,似乎 time.strptime() 不被认为是线程安全的。
有没有更好的方法从字符串创建日期时间对象?当前代码如下所示:
val = DateFromTicks(mktime(strptime(val, '%B %d, %Y')))
但是,当它在线程内运行时会产生异常。
谢谢!
Something I wrote throws a lot of AttributeError
exceptions when using time.strptime() inside a thread. This only seems to happen on Windows (not on Linux) but whatever- upon a'Googling, it seems that time.strptime() isn't considered thread-safe.
Is there a better way to create a datetime object from a string? Current code looks like:
val = DateFromTicks(mktime(strptime(val, '%B %d, %Y')))
But, that yields the exceptions as it's run inside a thread.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
根据 错误报告,如果您调用
strptime
一次,就不会发生这种情况在创建线程之前。我做了一些测试,似乎证实了这一点。因此,只需在初始化期间调用strptime
作为解决方法即可。According to the bug report, this doesn't happen if you call
strptime
once before creating your threads. I've done a little testing which seems to confirm this. So just make any call tostrptime
during initialization as a workaround.这是此错误的另一种解决方法,您可以简单地手动导入
_strptime
以及日期时间Just another workaround for this bug, you can simply import
_strptime
manually, along with datetime您是否尝试过自己手动同步?可能使用此配方中的同步装饰器。
Have you tried manually synchronizing it yourself? Possibly using the synchronization decorator from this recipe.
当我使用
import datetime
时,datetime.datetime.strptime()
不再抛出异常。When I use
import datetime
thedatetime.datetime.strptime()
does not throw the exception anymore.