---
title: "Apache的prefork模式导致飞信模块多次初始化"
author: "feisuzhu"
date: "2010-09-26T20:07:40Z"
tags: []
categories: ["编程语言"]
slug: "apache-prefork"
draft: false
lastmod: "2010-09-26T20:07:40Z"
id: 772526
url: "https://proton.iteye.com/blog/772526"
---

RT

自己写的一个模块，封装了一下PyFetion,目的是尽量保持飞信可用，再就是方便点。

在模块初始化的时候创建线程，登录，然后不停的查询数据库看是否有信息要发送。

后来发现很不稳定，看了下log

\[Sun Sep 26 19:59:36 2010] \[warn] child process 12374 still did not exit, sending a SIGTERM  
Error in my\_thread\_global\_end(): 1 threads didn't exit  
\[Sun Sep 26 19:59:38 2010] \[notice] caught SIGTERM, shutting down  
\[Sun Sep 26 20:02:24 2010] \[notice] Apache/2.2.14 (Ubuntu) PHP/5.3.2-1ubuntu4.5 with Suhosin-Patch mod\_ssl/2.2.14 OpenSSL/0.9.8k mod\_wsgi/2.8 Python/2.6.5 configured -- resuming normal operations  
\[Sun Sep 26 20:02:46 2010] \[error] &lt;SupervisorThread(SupervisorThread, started 140184746718992)&gt; ('construct...',)  
\[Sun Sep 26 20:02:49 2010] \[error] &lt;SupervisorThread(SupervisorThread, started 140184746718992)&gt; ('login...',)  
\[Sun Sep 26 20:02:54 2010] \[error] &lt;MessageThread(MessageThread, started 140184645531408)&gt; ('message round',)  
......  
\[Sun Sep 26 20:02:59 2010] \[error] &lt;SendSMSThread(SendSMSThread, started 140184637138704)&gt; ('nomsg',)  
\[Sun Sep 26 20:03:04 2010] \[error] &lt;SendSMSThread(SendSMSThread, started 140184637138704)&gt; ('nomsg',)  
.......  
\[Sun Sep 26 20:03:24 2010] \[error] &lt;SupervisorThread(SupervisorThread, started 140184746718992)&gt; ('construct...',)  
\[Sun Sep 26 20:03:24 2010] \[error] &lt;HeartbeatThread(HeartbeatThread, started 140184721078032)&gt; ('beat~',)  
\[Sun Sep 26 20:03:24 2010] \[error] &lt;SendSMSThread(SendSMSThread, started 140184637138704)&gt; ('nomsg',)  
\[Sun Sep 26 20:03:24 2010] \[error] &lt;HeartbeatThread(HeartbeatThread, started 140184721078032)&gt; ('beat done',)

 

怎么又开始初始化了呢？

 

后来发现是因为apache的prefork模式。在fork之前每个单独的进程都认为那个module没有初始化，于是就初始化了，导致n个进程同时登录。

 

现在在想，到底是把飞信做成webservice，还是干脆改用worker模式。