Duet3D Logo Duet3D
    • Tags
    • Documentation
    • Order
    • Register
    • Login

    Custom gcode upload function in python - RRF HTTP server options

    Scheduled Pinned Locked Moved
    Third-party software
    4
    5
    261
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • DuetUserundefined
      DuetUser
      last edited by

      I try to write a simple python software to upload gcode to a machine in async manner.

      I have it working synchronously by passing an iterator to request object, but I need async to send lots of files to different machines.

      A file is divided into chunks and each chunk is an async task.
      The problem is that subsequent chunks gets uploaded in separate POST requests as separate files, overwriting each other, so the uploaded file has last chunks size.

      I've set the X-File-Id header but it does not seem to work.

      So the question is is it at all possible to send one file in many POST requests to RRF?

      peter

      T3P3Tonyundefined chrishammundefined 2 Replies Last reply Reply Quote 0
      • T3P3Tonyundefined
        T3P3Tony administrators @DuetUser
        last edited by

        @DuetUser this is one for @chrishamm for how the http API works.

        www.duet3d.com

        1 Reply Last reply Reply Quote 0
        • chrishammundefined
          chrishamm administrators @DuetUser
          last edited by

          @DuetUser Appending data to existing files isn't possible via the HTTP API AFAIK. If the payloads are really small, I suppose you could send echo "your line" >> yourFile as a G-code request to the Duet and/or use that command to append a line to the last chunk to start the next one via M32.

          What exactly are you trying to achieve? Is it no option to write the file at once and to upload it to the Duet when it's ready?

          Duet software engineer

          DuetUserundefined 1 Reply Last reply Reply Quote 0
          • DuetUserundefined
            DuetUser @chrishamm
            last edited by DuetUser

            @chrishamm There is and I successfully do it by passing an iterator as "data" to POST request, it feeds the request with chunks of data. Everything is fine but it is a blocking IO. I'm trying to make it async, ond one of the ways is to pass chunks sequentially in subsequent POST requests, only RRF treats each chunk as a separate file.
            I have to dig into it more and perhaps make an async iterator and use it with async uploader, which I failed to do as of yet.

            This is a working non async upload code:

            import requests
            
            class upload_in_chunks(object):
                def __init__(self, filename, chunksize=1 << 13):
                    self.filename = filename
                    self.chunksize = chunksize
                    self.totalsize = os.path.getsize(filename)
                    self.readsofar = 0
            
                def __iter__(self):
                    with open(self.filename, 'rb') as file:
                        while True:
                            data = file.read(self.chunksize)
                            if not data:
                                sys.stderr.write("\n")
                                break
                            self.readsofar += len(data)
                            percent = self.readsofar * 1e2 / self.totalsize
                            sys.stderr.write("\r{percent:3.0f}%".format(percent=percent))
                            yield data
            
                def __len__(self):
                    return self.totalsize
            
            def sendme():
                requests.get(baseurl+'/rr_connect?password=reprap')
                requests.post(url, data=upload_in_chunks(afile, chunksize=10))
            
            

            peter

            ethanopundefined 1 Reply Last reply Reply Quote 0
            • ethanopundefined
              ethanop Banned @DuetUser
              last edited by

              This post is deleted!
              1 Reply Last reply Reply Quote 0
              • First post
                Last post
              Unless otherwise noted, all forum content is licensed under CC-BY-SA