The .NET Framework enables you to call any method asynchonously, by the two methods BeginInvoke and EndInvoke. The BeginInvoke method is used to register a function execution to Delegate(background threads), it quickly returns, so you can execute other codes after. In later time, you can use EndInvoke to get the return of the execution of that registered function, or you may had register a Callback function in the mean time you register that function, so this Callback will be automatically executed.
The structure of source code can be demonstrate in this steps:
- handle = BeginInvoke(my_function)
- do other work
- result = handle.EndInvoke()
- deal with result
Continue reading »
Written by ideawu
at 2010-08-28 17:00:29
The Python document says:
File objects on Windows are not acceptable, but sockets are. On Windows, the underlying select function is provided by the WinSock library, and does not handle file descriptors that don’t originate from WinSock.
In a recent project, I must continuously read from one network socket, forward to another socket, until the user input ‘q’ from the console. One may think to use threading, but I don’t want to deal with multi-thread stuffs. Finally, I found a none-clear solution, and it worked.
I create one thread – YES, thread, but no multi-thread concerns – this thread open a server socket listening on random port, then open a client socket connect to this server. The server socket accept the connection, then call sys.stdin.read() in a block way, all data read from stdin will write to that accepted connection. So client socket receive data reading from stdin. Now, the client socket is a *selectable* stdin, and it is thread-safe.
Download source code: stdio.py
Written by ideawu
at 2010-08-21 22:13:23 | tags: select, socket
Recent Comments