![](/img/trans.png)
[英]Writing multiple lists to the same .dat file simultaniously in Python 2.7?
[英]running script multiple times simultaniously in python 2.7
您好,我尝试多次运行脚本,但是我想同时使用子进程和线程将其同时进行,但是当我运行它时,它仍然看起来像是按顺序执行,有人可以帮忙吗我,这样我就可以让它一遍又一遍地运行相同的脚本? 它真的有效并且真的很慢吗?
编辑现在忘记了最后的代码
这是我到目前为止所拥有的
import os
import datetime
import threading
from subprocess import Popen
today = datetime.date.today()
os.makedirs("C:/newscript_image/" + str(today))
class myThread(threading.Thread):
def run(self):
for filename in os.listdir('./newscript/'):
if '.htm' in filename:
name = filename.strip('.htm')
dbfolder = "C:/newscript/db/" + name
os.makedirs(dbfolder)
Popen("python.exe C:/execution.py" + ' ' + filename + ' ' + name + ' ' + str(today) + ' ' + dbfolder)
myThread().start()
就个人而言,我将使用multiprocessing
。 我要编写一个函数,该函数需要一个文件名并execution
的主要任务(可能通过导入execution
并在其中运行某些函数):
import multiprocessing
import execution
import datetime
#assume we have a function:
#exection.run_main_with_args(filename,name,today_str,dbfolder)
today = datetime.datetime.today()
def my_execute(filename):
if '.htm' in filename:
name = filename.strip('.htm')
dbfolder = "C:/newscript/db/" + name
os.makedirs(dbfolder)
execution.run_main_with_args(filename,name,str(today),dbfolder)
p = multiprocessing.Pool()
p.map(my_execute,list_of_files_to_process)
跑一些快速测试。 使用脚本框架:
#!/usr/bin/env python
import os
import threading
from subprocess import Popen
class myThread(threading.Thread):
def run(self):
for filename in os.listdir("./newscript/"):
if '.htm' in filename:
Popen("./busy.sh")
myThread().start()
然后,我用一堆“ .htm”文件填充“ newscript”文件夹,以针对该文件运行脚本。
其中“ busy.sh”基本上是:
#!/usr/bin/env bash
while :
do
uptime >> $$
sleep 1
done
您拥有的代码确实的确触发了在后台运行的多个进程。 我使用包含200个文件的newscript文件夹进行了此操作,我看到200个进程全部在后台运行。
您注意到您希望它们同时在后台运行。
在大多数情况下,并行进程在后台“大致”并行运行,但是由于设置了大多数常用操作系统的方式,“并行”更像是“几乎并行”或更通常称为异步。 如果您非常仔细地查看访问时间,以这种方式产生的各种进程将轮流转,但它们永远不会同时做某事。
这是要注意的事情。 特别是由于您正在访问由操作系统和基础文件系统控制的文件。
对于您想做的事情:处理一堆入站文件,您的工作方式基本上是为每个出现的文件产生一个在后台处理文件的过程。
所呈现的逻辑存在两个问题:
建议:
与其生成工作,不如获取将要生成的文件处理代码并将其转换为Python函数。 将代码重新编写为守护进程,该守护进程将监视文件夹并跟踪生成的进程数,以便管理处理文件转换的后台进程级别。
处理文件时,您将剥离一个Python线程来处理该文件,这比产生OS级别的线程要轻得多。
详细说明了米尔森的答案:
假设我们有一个example1文件夹。
在example1内部,我们有两个python脚本:
execution.py和main.py
execution.py的内容如下所示:
import subprocess
def run_main_with_args(filename,name,today,dbfolder):
print('{} {} {}'.format('\nfilename: ',filename, ''))
print('{} {} {}'.format('name: ',name, ''))
print('{} {} {}'.format('today: ',today, ''))
print('{} {} {}'.format('dbfolder: ',dbfolder, ''))
outfile = dbfolder+ '/' + name + '.txt'
with open (outfile, 'w') as fout:
print('name', file=fout)
另外, main.py的内容如下所示:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Author : Bhishan Poudel; Physics Graduate Student, Ohio University
# Date : Aug 29, 2016
#
# Imports
import multiprocessing,os,subprocess
import datetime
import execution # file: execution.py
#assume we have a function:
#exection.run_main_with_args(filename,name,today_str,dbfolder)
today = datetime.datetime.today()
def my_execute(filename):
if '.txt' in filename:
name = filename.strip('.txt')
dbfolder = "db/" + name
if not os.path.exists(dbfolder): os.makedirs(dbfolder)
execution.run_main_with_args(filename,name,str(today),dbfolder)
p = multiprocessing.Pool()
p.map(my_execute,['file1.txt', 'file2.txt'])
然后,如果我们运行此main.py ,它将以并行方式在所需目录中创建所需文件!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.