473,396 Members | 2,018 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Python threading

I am wondering what happens to a thread in python in relation to
win32com extensions.

If I create a new thread, that uses the Dispatch method from win32com,
what happens to the memory allocated in that thread when the thread is
done. Will the Dispatch release the memory it created, or will the
memory remain?

The problem rises from the fact that Dispatch does not seem to release
memory correctly every time. If I include the commands in a thread by
themselves, will the thread clean up ALL memory it used after it is
done?

I did try the pythoncom.CoUnitialize() to release memory, but it
doesn't seem to work (it does work about 30-45 seconds after the
command is run).

Any input is greatly appreciated (on the thread issue or how to use
the pythoncom.CoUnitiliaze() to make it release memory right away).

Thank you in advance!

Mar 8 '07 #1
3 3414
En Thu, 08 Mar 2007 14:25:02 -0300, <te*****@gmail.comescribió:
I am wondering what happens to a thread in python in relation to
win32com extensions.

If I create a new thread, that uses the Dispatch method from win32com,
what happens to the memory allocated in that thread when the thread is
done. Will the Dispatch release the memory it created, or will the
memory remain?

The problem rises from the fact that Dispatch does not seem to release
memory correctly every time. If I include the commands in a thread by
themselves, will the thread clean up ALL memory it used after it is
done?
All threads share the same memory space, there is not a "per-thread"
memory allocator, if that's what you are thinking.
Perhaps you hold a reference to some objects in the Thread object? Or you
still keep the Thread object itself?
I did try the pythoncom.CoUnitialize() to release memory, but it
doesn't seem to work (it does work about 30-45 seconds after the
command is run).
I don't understand what you say here.
What means "it doesn't seem to work" and "it does work 30 seconds after"?
Any input is greatly appreciated (on the thread issue or how to use
the pythoncom.CoUnitiliaze() to make it release memory right away).
What memory do you want to release "right away"?

--
Gabriel Genellina

Mar 9 '07 #2
On Mar 8, 6:15 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.arwrote:
En Thu, 08 Mar 2007 14:25:02 -0300, <test...@gmail.comescribió:
All threads share the same memory space, there is not a "per-thread"
memory allocator, if that's what you are thinking.
Perhaps you hold a reference to some objects in the Thread object? Or you
still keep the Thread object itself?
That is what I was thinking. A per-thread memory allocator of some
sort. What I need is run a lot of commands from a COM object that can
be run independently. There commands leak memory, so when the command
finishes, the memory consumption jumps to 800MBs or so. Which begs the
question, if I initialize the COM object in a separate thread and run
the command, does finishing the thread clean up any and all memory the
thread used or not. The output of each thread would actually be a file
on disk, therefore there is no need to pass any data between the
threads and the main program.
I did try the pythoncom.CoUnitialize() to release memory, but it
doesn't seem to work (it does work about 30-45 seconds after the
command is run).

I don't understand what you say here.
What means "it doesn't seem to work" and "it does work 30 seconds after"?
pythoncom.CoUninitialize(), if I am not mistaken, releases a COM
object (and therefore the memory it uses I assume). When the command
is run (in PythonWin for example), the memory is not released but
persists for quite a while. Using my watch and the Task Manager in
Windows I can see that the memory is released approximately 30 seconds
AFTER I run the pythoncom.CoUninitialize() command is run.
What memory do you want to release "right away"?
The memory I want to release is the memory the COM object used (the
one initialized with the win32com Dispatch command). The "right-away"
is not much of an issue, but if I can release it before I run each
command from the COM object that leaks memory, it would be nice.
Running upwards to 800MBs of RAM for one 500 line python script seems
a little bit too much for me.

Thank you for your reply Gabriel. If you have any more input, I would
greatly appreciate it.
Mar 9 '07 #3
En Fri, 09 Mar 2007 00:38:55 -0300, <te*****@gmail.comescribió:
pythoncom.CoUninitialize(), if I am not mistaken, releases a COM
object (and therefore the memory it uses I assume).
Not exactly. *You* must release the COM object (usually assigning None to
all references to it). CoUninitialize releases all system resources held
by the COM application, unloads DLLs, and so; it *may* release all COM
objects but I'm not sure.
When the command
is run (in PythonWin for example), the memory is not released but
persists for quite a while. Using my watch and the Task Manager in
Windows I can see that the memory is released approximately 30 seconds
AFTER I run the pythoncom.CoUninitialize() command is run.
If it is eventually released, you don't have a problem, right?
And, if it will be needed again after a while (when you run the next
command), it looks better this way.
There are many layers on memory management: the OS manages 4K pages that
are mapped on the process address space; the C runtime suballocates such
pages as memory blocks; Python itself has two levels of memory allocation
(and I don't know the details). It's not easy to know exactly how much
memory is actually used; I think the Python runtime, once it allocates a
memory block, never releases it to the OS (remaining as a free block,
available for further usage).

And in general, DLLs are *not* unloaded as soon as its usage count goes to
0; there is certain delay. (I don't remember, but I think there is a
registry key called DLLUnloadTime or something). The idea is to avoid
removing it from memory just to load it again a few moments later, so the
unloading is delayed for a few seconds. You may be seeing this effect.
>What memory do you want to release "right away"?

The memory I want to release is the memory the COM object used (the
one initialized with the win32com Dispatch command). The "right-away"
is not much of an issue, but if I can release it before I run each
command from the COM object that leaks memory, it would be nice.
Running upwards to 800MBs of RAM for one 500 line python script seems
a little bit too much for me.
Let's see if I can understand the problem. You run multiple commands on a
COM object, one after another. The COM object is buggy and leaks some
memory. You run each command on a separate thread, with a CoInitialize(),
waiting for completion, and a CoUninitialize() at the end. Memory usage
grows to 800MB when you invoke each command, but if you wait enough time
after the thread finalizes, memory usage goes down to normal. After that,
you run the next command, and so on.
Or do you wait until the whole program finalizes, and 30 seconds after
that, memory usage drops?

--
Gabriel Genellina

Mar 9 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

65
by: Anthony_Barker | last post by:
I have been reading a book about the evolution of the Basic programming language. The author states that Basic - particularly Microsoft's version is full of compromises which crept in along the...
19
by: Jane Austine | last post by:
As far as I know python's threading module models after Java's. However, I can't find something equivalent to Java's interrupt and isInterrupted methods, along with InterruptedException....
7
by: kanzen | last post by:
I keep telling my friends that Python rocks. Now it's time to put my money where my mouth is. I'm about to start writing a server for a phone based game. It needs to handle simlpe requests from...
3
by: David Harrison | last post by:
I am working on an application on Mac OS X that calls out to python via PyImport_ImportModule(). I find that if the imported module creates and starts a python thread, the thread seems to be...
0
by: Samuel M. Smith | last post by:
I can build python 2.4.2 from source on the embedded linux box when I nfs mount and boot a full debian distribution. The embedded box also has stripped down linux distribution in onboard flash....
1
by: Carl J. Van Arsdall | last post by:
Hey everyone, I know I've posted several questions regarding python and python's parallel capabilities so bear with me as I've never attempted to incite discussion. However, today I'm...
9
by: cgwalters | last post by:
Hi, I've recently been working on an application which does quite a bit of searching through large data structures and string matching, and I was thinking that it would help to put some of this...
0
by: Ling | last post by:
I am using boost.python to wrap C++ function which includes directmusic libraries to simply play the midi, but lots of linkage errors "error LNK2001: unresolved external symbol". I wonder if it is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.