Mac上python多线程错误:...+[__NSPlaceholderDate initialize]...

Posted leocll

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mac上python多线程错误:...+[__NSPlaceholderDate initialize]...相关的知识,希望对你有一定的参考价值。

错误提示

objc[27880]: +[__NSPlaceholderDate initialize] may have been in progress in another thread when fork() was called.
objc[27880]: +[__NSPlaceholderDate initialize] may have been in progress in another thread when fork() was called. We cannot safely call it or ignore it in the fork() child process. Crashing instead. Set a breakpoint on objc_initializeAfterForkError to debug.

错误原因

刚开始以为是代码的问题,后来才发现其实是macOS的High Sierra以上的操作系统对多线程的限制。

解决办法

在环境变量中添加export OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES

  • 终端
// 零时解决方法
// 在终端中执行以下命令,接着继续执行python程序
export OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES
// 永久解决方案
// 终端中执行以下命令
sudo echo "export OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES" >> ~/.bash_profile && source ~/.bash_profile
  • PyCharm
Run -> Edit Configuations... -> Environment variables  
在上面的选择中添加`OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES`,重启`PyCharm`即可

技术图片
技术图片

以上是关于Mac上python多线程错误:...+[__NSPlaceholderDate initialize]...的主要内容,如果未能解决你的问题,请参考以下文章

python_多线程多进程

iOS - 系统方法中弃用的关键字的了解 NS_AVAILABLE和NS_DEPRECATED

Python:多线程上的 CPU 密集型任务

python 多线程 及多线程通信

Python爬虫(十八)_多线程糗事百科案例

Python爬虫(十八)_多线程糗事百科案例