线程安全相当于python的time.strptime()?

发布于 2024-08-24 09:19:31 字数 299 浏览 4 评论 0原文

当我在线程中使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

游魂 2024-08-31 09:19:31

根据 错误报告,如果您调用 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 to strptime during initialization as a workaround.

Hello爱情风 2024-08-31 09:19:31

这是此错误的另一种解决方法,您可以简单地手动导入 _strptime 以及日期时间

import _strptime
from datetime import datetime

# then, in threaded block
datetime.strptime(date, format)

Just another workaround for this bug, you can simply import _strptime manually, along with datetime

import _strptime
from datetime import datetime

# then, in threaded block
datetime.strptime(date, format)
花桑 2024-08-31 09:19:31

您是否尝试过自己手动同步?可能使用此配方中的同步装饰器。

Have you tried manually synchronizing it yourself? Possibly using the synchronization decorator from this recipe.

夏尔 2024-08-31 09:19:31

当我使用 import datetime 时,datetime.datetime.strptime() 不再抛出异常。

When I use import datetime the datetime.datetime.strptime() does not throw the exception anymore.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文