Return str to a callback raise a segfault if used in string formating

Chris Angelico rosuav at gmail.com
Fri Oct 13 03:23:04 EDT 2017


On Fri, Oct 13, 2017 at 4:46 PM, Vincent Vande Vyvre
<vincent.vande.vyvre at telenet.be> wrote:
> Simplified code:
>
> ---%<------------------------------------------
> class Foo:
>     tasks = []
>     def process(self, *args):
>         # process with the file
>         # save result on disk with a new name
>         self.out_filename.write(.....)
>
>     def run_multitasks(self):
>         while self.tasks:
>             t = Thread(target=process, args=self.tasks.pop(0))
>             t.start()
>             t.join()
>             self.callback(self.out_filename)
>
>     def add_task(self, fname, callback):
>         self.tasks.append(fname)
>         self.callback = callback
>         if len(self.tasks) == 1:
>             self.run_multitasks()
>
> def slot(filename):
>     print(filename)                 # Works
>     print("%s created" % filename)  # Segfault !
>
> Files = [list of files]
> foo = Foo()
> for f in Files:
>     foo.add_task(f, slot)
> ---%<------------------------------------------
>
> If I place self.callback() at the end of the func process that doesn't
> change anything.

First off, exactly what version of Python are you running this under?
With a segfault, you need to be pretty specific - platform, version,
word size, as much as you can gather.

Secondly: Can you create a version of this that doesn't comment out
part of the work? If you can make a script where, any time you run it,
Python segfaults, that would be extremely helpful.

Your code currently looks a bit weird. You create a thread, start it,
and then immediately wait for it (join()). When you add a task, if
it's the first task you've added, you process tasks, thus removing
that task. So the class isn't actually doing anything, and logically,
you could simply process the files directly in the loop. I'm guessing
that something in there (probably the threading) is causing the crash,
but without a complete and testable demo, it's hard to be sure.

ChrisA



More information about the Python-list mailing list