Python中的time.sleep和多线程问题 [英] Issues with time.sleep and Multithreading in Python

查看:368
本文介绍了Python中的time.sleep和多线程问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在python中的time.sleep()函数遇到问题.我正在运行一个脚本,该脚本需要等待另一个程序来生成txt文件.虽然,这是一台非常古老的机器,所以当我休眠python脚本时,我遇到了其他程序无法生成文件的问题.除了使用time.sleep(),还有其他选择吗?我认为锁定线程可能会起作用,但本质上,这只是将线程锁定几秒钟的循环.我将在此处提供我所做工作的一些伪代码.

I am having an issue with the time.sleep() function in python. I am running a script that needs to wait for another program to generate txt files. Although, this is a terribly old machine, so when I sleep the python script, I run into issues with the other program not generating files. Is there any alternatives to using time.sleep()? I thought locking the thread might work but essentially it would just be a loop of locking the thread for a couple of seconds. I'll give some pseudo code here of what I'm doing.

While running:
    if filesFound != []:
         moveFiles
    else:
       time.sleep(1)

推荐答案

进行非阻塞等待的一种方法是使用 threading.Event :

One way to do a non-blocking wait is to use threading.Event:

import threading
dummy_event = threading.Event()
dummy_event.wait(timeout=1)

这可以是来自另一个线程的set(),表示已完成某些操作. 但是,如果您在另一个线程中执行操作,则可以完全避免超时和事件,而只需join另一个线程:

This can be set() from another thread to indicate that something has completed. But if you are doing stuff in another thread, you could avoid the timeout and event altogether and just join the other thread:

import threading

def create_the_file(completion_event):
    # Do stuff to create the file

def Main():
    worker = threading.Thread(target=create_the_file)
    worker.start()

    # We will stop here until the "create_the_file" function finishes
    worker.join()

    # Do stuff with the file

如果您想要使用事件进行更细粒度控制的示例,我可以向您展示...

If you want an example of using events for more fine-grained control, I can show you that...

如果您的平台不提供线程模块,则线程方法将不起作用.例如,如果尝试替换dummy_threading模块,则dummy_event.wait()立即返回.不确定join()方法.

The threading approach won't work if your platform doesn't provide the threading module. For example, if you try to substitute the dummy_threading module, dummy_event.wait() returns immediately. Not sure about the join() approach.

如果您正在等待其他进程完成,最好使用 wait 方法,以确保在完成进一步工作之前已完成该过程.)

If you are waiting for other processes to finish, you would be better off managing them from your own script using the subprocess module (and then, for example, using the wait method to be sure the process is done before you do further work).

如果您无法通过脚本管理子流程,但知道PID,则可以使用 os.waitpid() 函数.如果在使用此功能时该过程已经完成,请当心OSError ...

If you can't manage the subprocess from your script, but you know the PID, you can use the os.waitpid() function. Beware of the OSError if the process has already finished by the time you use this function...

如果您希望以跨平台的方式查看目录以获知新文件,建议您使用 PyGTK/PyGObject .您可以使用 GIO.File 的"> monitor_directory 方法

If you want a cross-platform way to watch a directory to be notified of new files, I'd suggest using a GIO FileMonitor from PyGTK/PyGObject. You can get a monitor on a directory using the monitor_directory method of a GIO.File.

目录监视的快速示例代码:

Quick sample code for a directory watch:

import gio

def directory_changed(monitor, file1, file2, evt_type):
    print "Changed:", file1, file2, evt_type

gfile = gio.File(".")
monitor = gfile.monitor_directory(gio.FILE_MONITOR_NONE, None)
monitor.connect("changed", directory_changed) 

import glib
ml = glib.MainLoop()
ml.run()

这篇关于Python中的time.sleep和多线程问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆