具有“While True”的子进程在3640次迭代后结束

2024-05-19 21:56:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个Django应用程序,每当有一个数据库插入时就会产生一个子进程。你知道吗

型号.py

    # spawn subprocess to trigger tweepy
# output of subprocess DOES NOT log to the console.
def tweepy_tester(sender, **kwargs):
    if kwargs['created']:
        logger.error('tweepy trigger-start!')
        p = subprocess.Popen([sys.executable, "/Users/viseshprasad/PycharmProjects/Blood_e_Merry/loginsignup/tests.py"],
                             stdout=subprocess.PIPE,
                             stderr=subprocess.STDOUT)
        logger.error('tweepy trigger-over!')


# use post_save to trigger tweepy later
post_save.connect(tweepy_tester, sender=User)

测试.py

logger = logging.getLogger(__name__)


# Create your tests here.

def for_thread():
    i = 0
    while True:
        f = open('test.txt', 'a')
        f.write('Tweepy triggered ' + str(i) + '\n')  # python will convert \n to os.linesep
        f.close()  # you can omit in most cases as the destructor will call it
        i += 1


for_thread()

触发器发生得很好,但子进程只向写入3640行测试.txt文件,即使我使用了while True: 我基本上是在寻找一个子进程,在触发器之后不间断地运行,作为一个独立的线程,并且不干扰主线程。你知道吗

目的: 我用通常的python manage.py runserver运行我的应用程序。 用户注册->;数据库插入->;触发我的tweepy实现,它保持推特流并在不同的后台线程上不间断地分析推特,以免干扰注册过程。你知道吗

以上测试就是为了这个目的。 感谢您的帮助。我们也欢迎为实现这一点提出任何其他建议。你知道吗

谢谢。你知道吗


Tags: thetopy数据库应用程序进程deferror