2017-01-18 35 views
1

我正在研究一個Django項目(Django 1.10,Python 3.4.3),其中我試圖上傳python子進程中的文件。我需要上傳文件,並在上傳文件時計算一些總和,並且我們希望系統在這樣做時仍然可用,所以我們認爲將這些任務放在子進程中是可行的。在python子進程中上傳文件

現在,我正在努力讓文件上傳。

第一次嘗試:

應用程序/視圖/ UploadView.py

class NewUploadView(TemplateView): 
    template_name = "../templates/simulation/upload.html" 

    @transaction.atomic 
    def post(self, request): 
     if request.method == 'POST': 
      if not request.FILES['input_file']: 
       return HttpResponseBadRequest("No 'input_file' is provided") 
      else: 
       sim_name = self.request.POST.get(u"name", None) 

       p = Popen(["/.virtualenvs/env/bin/python3.4", "manage.py", 
        "load_simulation", str(request.FILES['input_file'], 
        str(sim_name)]) 

       p.communicate() 

       # Redirect to appropriate page 
       HttpResponseRedirect(reverse('home.display_simulations'))` 

應用程序/管理/命令/ load_simulation.py

import csv 
import os 

from django.conf import settings 
from django.core.management.base import BaseCommand 

from website.apps.home.models import Simulation, Location, Data, SimulationModel 


class Command(BaseCommand): 
    help = 'Upload csv data file and save objects in database' 

    def add_arguments(self, parser): 
     parser.add_argument("my_file", type=str) 
     parser.add_argument("simulation_name", type=str) 

    def handle(self, *args, **options): 
     """ 
     Upload data file 
     :param args: 
     :param options: 
     :return: 
     """ 
     my_file = options["my_file"] 
     simulation_name = options["simulation_name"] 

     # Save the simulation object, with the data_file 
     simulation = Simulation.objects.create(name=simulation_name, data_file=my_file) 
     simulation.save() 

     filename = os.path.join(settings.MEDIA_ROOT, "simulation_files", simulation.getfilename()) 
     file_obj = open(filename, 'r') 
     dictreader = csv.DictReader(file_obj) 
     line = None 

     for line in dictreader: 
      location = Location.objects.filter(
       department_code=line['department_code'], 
       municipality_code=line['municipality_code'], 
      ).first() 

      if not location: 
       location = Location.objects.create(
        department=line['department'], 
        department_code=line['department_code'], 
        municipality=line['municipality'], 
        municipality_code=line['municipality_code'], 
       ) 

      Data.objects.create(
       location=location, 
       date=line['date'].split(" ")[0], # Assuming date is in "YYYY-MM-DD HH:MM:SS" format 
       simulation=simulation, 
       value_low=line['value_low'], 
       value_mid=line['value_mid'], 
       value_high=line['value_high'], 
      ) 

     simulation.is_uploaded = True 
     simulation.save() 
     print("DONE WITH LOAD SIMULATION!") 

我有設置,以便該文件將被保存在/media/simulation_files目錄。但該文件不會保存到服務器,這意味着它在load_simulation.py文件中失敗,因爲該文件不在該位置,因此該文件嘗試打開該文件。

然後我意識到,我傳遞一個TemporaryFile作爲一個字符串,所以也許這是問題的一部分。

第二次嘗試

我改變了部分UploadView.py把該文件作爲標準輸入英寸

 p = Popen(["/.virtualenvs/env/bin/python3.4", "manage.py", 
           "load_simulation", str(sim_name), is_historical], 
           stdin=PIPE) 

     p.communicate(input=request.FILES['output_file']) 

和修改load_simulation.py開始如下:

class Command(BaseCommand): 
    help = 'Upload csv data file and save objects in database' 

    def add_arguments(self, parser): 
     parser.add_argument("my_file", type=str) 
     parser.add_argument("simulation_name", type=str) 

    def handle(self, *args, **options): 
     """ 
     Upload data file 
     :param args: 
     :param options: 
     :return: 
     """ 

     simulation_name = options["simulation_name"] 

     my_file = input() 

     # Save the simulation object, with the data_file 
     simulation = Simulation.objects.create(name=simulation_name, data_file=my_file.name) 
     simulation.save() 

     filename = os.path.join(settings.MEDIA_ROOT, "simulation_files", simulation.getfilename()) 
     file_obj = open(filename, 'r') 
     dictreader = csv.DictReader(file_obj) 

     ... same as before ...  

這是我從下面的問題/答案了: Python3 subprocess communicate example

這給了我一個錯誤

TypeError: 'InMemoryUploadedFile' does not support the buffer interface

我是新來的子流程的工作,並在接下來嘗試嘗試。我意識到p.communicate()可能並不是這裏的最佳選擇,但我希望這可以使基本功能失效,然後我可以改進它。建議和意見是非常感謝!

回答

0

好的,我解決了在子進程中上傳文件的基本問題。當創建Simulation對象時,該文件被保存到/media/simulation_files。出於某種原因,當Simulation保存在子流程中時,這不起作用,所以我在創建子流程之前將其移至視圖並保存了該對象。

然後,Popen只需要新創建的Simulation的id作爲參數,以便訪問整個模擬(包括數據文件)。

重要

我不得不否則除去@transaction.atomic裝飾,在視圖中創建的Simulation對象將不會在子進程訪問。

TL;博士版本:

UploadView.py

class NewerUploadView(TemplateView): 
    template_name = "../templates/simulation/upload.html" 

    def post(self, request): 
     if request.method == 'POST': 
      if not request.FILES['output_file']: 
       return HttpResponseBadRequest("No 'output_file' is provided") 
      else: 
       sim_name = self.request.POST.get(u"name", None) 

       # Create the Simulation here to save the file 
       simulation = Simulation.objects.create(name=simulation_name, 
                 data_file=fp) 
       simulation.save() 

       # Get the new Simulation object 
       new_sim = Simulation.objects.get(id=sim_id) 

       p = Popen(["/.virtualenvs/env/bin/python3.4", "manage.py", 
        "load_simulation", str(new_sim.id)], stdout=PIPE, stderr=PIPE) 
       p.communicate() 

       # Redirect to appropriate page 
       return HttpResponseRedirect(reverse('home.display_simulations')) 

load_simulation.py

class Command(BaseCommand): 
    help = 'Upload csv data file and save objects in database' 

    def add_arguments(self, parser): 
     parser.add_argument("sim_id", type=int) 

    def handle(self, *args, **options): 
     """ 
     Upload data file 
     :param args: 
     :param options: 
     :return: 
     """ 

     sim_id = options["sim_id"] 
     sim = Simulation.objects.get(id=sim_id) 
     filename = os.path.join(settings.MEDIA_ROOT, sim.data_file.name) 
     file_obj = open(filename, 'r') 
     dictreader = csv.DictReader(file_obj) 
     line = None 

     for line in dictreader: 

      location = Location.objects.filter(
       department_code=line['department_code'], 
       municipality_code=line['municipality_code'], 
      ).first() 

      if not location: 
       location = Location.objects.create(
        department=line['department'], 
        department_code=line['department_code'], 
        municipality=line['municipality'], 
        municipality_code=line['municipality_code'], 
       ) 

      Data.objects.create(
       location=location, 
       date=line['date'].split(" ")[0], # Assuming "YYYY-MM-DD HH:MM:SS" 
       simulation=sim, 
       value_low=line['value_low'], 
       value_mid=line['value_mid'], 
       value_high=line['value_high'], 
      ) 
     sim.is_uploaded = True 
     sim.save() 
     print("DONE WITH LOAD SIMULATION!")