473,507 Members | 3,678 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3421
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
6663
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
6442
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
1488
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
1485
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
1760
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
8498
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
1836
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
1534
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
7110
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
7482
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...
0
5623
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5041
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
411
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.