473,574 Members | 2,700 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calling a thread asynchronously with a callback

I'm a C# developer and I'm new to Python. I would like to know if the concept of Asynchronous call-backs exists in Python. Basically what I mean is that I dispatch a thread and when the thread completes it invokes a method from the calling thread. Sort event driven concept with threads.
Thanks.

Ed Gomez
Nov 27 '06 #1
4 4790
Edwin Gomez wrote:
I'm a C# developer and I'm new to Python. I would like to know if the
concept of Asynchronous call-backs exists in Python. Basically what I
mean is that I dispatch a thread and when the thread completes it invokes
a method from the calling thread. Sort event driven concept with threads.
Counter question: does such a thing exist in C#, and if, is it bound to some
existing event loop?

I'm really curious, because having code being interrupted at any time by a
asynchronous callback strikes me as dangerous. But then maybe I'm just a
whimp :)

Diez
Nov 27 '06 #2
Edwin Gomez wrote:
I'm a C# developer and I'm new to Python. I would like to know if
the concept of Asynchronous call-backs exists in Python.
Sure. Either with this:

http://twistedmatrix.com/projects/co...wto/async.html

Or manually using select().
Basically what I mean is that I dispatch a thread and when the
thread completes it invokes a method from the calling thread.
Sort event driven concept with threads.
What you describe here isn't the only way to do asynchronous
callbacks. See above for a simpler way.

Regards,
Björn

--
BOFH excuse #233:

TCP/IP UDP alarm threshold is set too low.

Nov 27 '06 #3

Diez B. Roggisch wrote:
Edwin Gomez wrote:
I'm a C# developer and I'm new to Python. I would like to know if the
concept of Asynchronous call-backs exists in Python. Basically what I
mean is that I dispatch a thread and when the thread completes it invokes
a method from the calling thread. Sort event driven concept with threads.

Counter question: does such a thing exist in C#, and if, is it bound to some
existing event loop?

I'm really curious, because having code being interrupted at any time by a
asynchronous callback strikes me as dangerous. But then maybe I'm just a
whimp :)
I've used them with Windows Forms,. The callback puts a message in the
event loop. It doesn't actually interrupt the code.

Fuzzyman
http://www.voidspace.org.uk/python/index.shtml
Diez
Nov 27 '06 #4
"Diez B. Roggisch" <de***@nospam.w eb.dewrote:
Edwin Gomez wrote:
>I'm a C# developer and I'm new to Python. I would like to know if
the concept of Asynchronous call-backs exists in Python. Basically
what I mean is that I dispatch a thread and when the thread completes
it invokes a method from the calling thread. Sort event driven
concept with threads.

Counter question: does such a thing exist in C#, and if, is it bound
to some existing event loop?
Yes, or rather the concept exists in .Net. C# and other .Net languages
don't actually allow pointers to functions, instead they implement
callbacks using what are called Delegates: for each different function
signature you need a new Delegate class, but the language compilers
automate the process so that using Delegates becomes very similar to using
function pointers.

In early C#:

// Delegate for function with no args and no results
public delegate void SimpleDelegate( );
....
SimpleDelegate simpleDelegate = new SimpleDelegate( somefunc);
SimpleDelegate simpleDelegate = new SimpleDelegate( someobj.somefun c);
....
simpleDelegate( );

C# 2.0 added the ability to declare delegates inline (i.e. anonymous
functions), and delegate inferencing to avoid writing 'new delegatename'
everywhere:

SimpleDelegate simpleDelegate = somefunc;

Where delegates start becoming more interesting than just function pointers
is that all delegates support both multicasting and asynchronous callbacks.

You can add any number of functions into a delegate, and you can also
remove functions from delegates.

Asynchronous callbacks are done by calling a delegate's BeginInvoke method.
You have to pass this the same arguments as you would when calling the
delegate directly, plus an AsyncCallback delegate and an object to be
passed as part of the asynchronous response which is usually the original
delegate. From the callback you can then call EndInvoke to get the result
returned from the call to the function.

Now the bit you asked about: the callback happens on an arbitrary thread.

The .Net runtime maintains a thread pool to use for this sort of callback
so it doesn't have the overhead of setting up a new thread every time. A
lot of the system library classes support similar Beginxxx/Endxxx function
pairs for potentially lengthy operations such as reading from a Stream, or
performing a web request and the thread pool is also used for these.

It is fairly easy to implement a similar scheme in Python, just write a
thread pool which gets function/argument/callback combinations from a
queue, calls the function and then calls the callback with the response.
I'm really curious, because having code being interrupted at any time
by a asynchronous callback strikes me as dangerous. But then maybe I'm
just a whimp :)
Not at all. It comes as a surprise to some people that there is no such
thing in .Net as a single threaded program:

As well as asynchronous delegates the garbage collector uses separate
threads: what it does is to block all threads from running while it sweeps
the memory, compacting all moveable objects and releasing any unreferenced
objects without finalizers. Unreferenced objects with finalizers are
resurrected onto a list and then the garbage collector lets other threads
run. A background thread is then used to call the finalizers for the
collected objects and each finalizer is cleared once it has executed (so
that unless resurrected, the next garbage collection of an appropriate
generation can overwrite the object). What this means is that *all*
finalizers in .Net will run on a different thread (and in arbitrary order):
of course finalizers are pretty crippled so this may not matter.
Nov 28 '06 #5

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

Similar topics

1
1702
by: COMfused | last post by:
I have a thread that has a delegate function which will be called when another process exits. This thread went to sleep, the process exits and is supposed to call the delegate, but it did not happen. When the thread wake up, the function is not called either. The question is, will a thread ignore all the callback when it is sleeping?
10
1905
by: Fernando Rodríguez | last post by:
Hi, Assume I have Main function that looks like this: static void Main(string args) { Action h1 = new Action(0);
5
1544
by: Marty | last post by:
Hi, When creating a thread, how do I make it loop until a specific condition happen to make it end? Just for instance, I want a thread to loop until (appl. is closed) to perform actions on items within a collection. Does it need a callback function?
11
7308
by: GVN | last post by:
Hi All, Can anyone guide me when asynchronous method calls will be benificial? Are there any disadvantages of using asynchronous calls? Thanks,
0
1244
by: VincentJS | last post by:
Greetings fellow programmers! Could someone explain to me HOW to call a mousehook OUTSIDE the calling thread (or program). Microsoft says following: HHOOK SetWindowsHookEx( int idHook, HOOKPROC lpfn, HINSTANCE hMod, DWORD dwThreadId
4
11662
by: Rainer Queck | last post by:
Hi NG, I am currently exploring VS2008 and WPF. In a very little WPF-Aplication I am using a System.Timers.Timer and assigned a Eventhandler to its "Elapsed" event. When the eventhandler is called, I receive the following error message: The calling thread must be STA, because many UI components require this. I assum I need to...
2
1719
by: StefanPienaar | last post by:
Hi I was recently given the task of converting an Asp.net 1.1 portal to Asp.net 3.5. The problem is it includes a webservice and a lot of the methods are called asynchronously. Now in .net 1.1 the stub creates the .Begin and .End methods for you and in Asp.net 2.0 it creates the .Asyn method when you create a reference to the webservice. ...
7
2223
by: Daniel | last post by:
How does .NET in VC++ create a new thread. I need to be able to run a procedure within a thread that will notify me when that procedure is done so that I can continue with the next procedure. I can't go to the next procedure until the first one is completely through. I believe it is called a callback. Daniel
0
8066
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8249
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7826
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8106
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6461
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5631
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3743
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3755
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1066
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.