multithreading in python

Steven D'Aprano steve at pearwood.info
Tue Aug 13 04:38:01 EDT 2013


On Tue, 13 Aug 2013 01:06:01 -0700, samaneh.yahyapour wrote:

> hi
> my program work by 4 thread but when i use more thread it terminates

Is that a problem? Isn't it supposed to terminate, when it has finished?

If it raises an exception, or crashes, you should tell us.



> i use opencv in my image_process.so
> 
> my code is :
> 
> 
> #!/usr/bin/python
> import sys
> import os
> import io
> import time
> import copy
> import threading
> import ctypes
> 
> class MyClass():
>     
>     def __init__(self):
>         i = 0
>         while i<10:
>             thread1 = threading.Thread(target=self.item_thread)
>             thread1.start()
>             i = i+1
>             time.sleep(0.01)


This is better written as:

    def __init__(self):
        self.threads = []
        for i in range(10):
            thread = threading.Thread(target=self.item_thread)
            thread.start()
            self.threads.append(thread)
            time.sleep(0.01)  # not sure this helps for anything


I think it will also help if you keep references to the threads. That 
will stop them from being garbage collected unexpectedly, and you can 
check their status before exiting the main thread.


>     def item_thread(self):
>         imageAnalyzer=ctypes.CDLL("../so/image_process.so")
>         imageAnalyzer.aref_img_score_init("/opt/amniran/etc/face.xml",
>         "/opt/amniran/etc/porn.xml") for filename in
>         os.listdir("../script/images/"):
>             if filename[-4:] == ".jpg" or filename[-4:] == ".png" or
>             filename[-4:] == ".gif" or filename[-5:] == ".jpeg"    :
>                 
>                 path = "../script/images/%s"%filename
>                 
>                 fo = file(path, "r")
>                 content = fo.read()
>                 score = imageAnalyzer.score_image(content, len(content))
>                 print "%d : %s " %(score, path)
>         print "ENDDDDDDDDDDDDDDDDDDDDDDDDDDDD"
>                 
>    
> x = MyClass()


I suspect that when the main thread exits, and your other threads are 
still running, you may be in trouble. But I'm not a threading expert, so 
I could be wrong. However, I would put something like this at the end:

for thread in x.threads:
    x.join()


that way the main thread cannot finish until each of the subthreads are.



-- 
Steven



More information about the Python-list mailing list