473,382 Members | 1,745 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,382 software developers and data experts.

cgi "print statement" in multithreaded enviroment?


How can i use cgi'like print statement in a multitreaded web framework?
each thread has its own Servlet instance with request/response objects,
sys.stdout = self.response(which is a file like object) wont work because
all threads will set the same file object and it will be a concurrence
mess.

I am out of ideas here,so any hacks welcome =)

Jul 19 '05 #1
5 1860
vegetax wrote:
How can i use cgi'like print statement in a multitreaded web framework?
each thread has its own Servlet instance with request/response objects,
sys.stdout = self.response(which is a file like object) wont work because
all threads will set the same file object and it will be a concurrence
mess.

I am out of ideas here,so any hacks welcome =)


instead of:

print "<html>.....</html>

do:

print >>self.response, "<html>....</html>"
--Irmen
Jul 19 '05 #2
Irmen de Jong wrote:
vegetax wrote:
How can i use cgi'like print statement in a multitreaded web framework?
each thread has its own Servlet instance with request/response objects,
sys.stdout = self.response(which is a file like object) wont work because
all threads will set the same file object and it will be a concurrence
mess.

I am out of ideas here,so any hacks welcome =)


instead of:

print "<html>.....</html>

do:

print >>self.response, "<html>....</html>"
--Irmen


But i want to use "print" as a commodity feature, print >>
self.response,'html..' is longer to type than
self.response.write('html..')

The only clear way i am thinking right now is to write a preprocessor ,like
quixote ptl,but thats sloww
Jul 19 '05 #3
vegetax <ve******@gmail.com> writes:
But i want to use "print" as a commodity feature, print >>
self.response,'html..' is longer to type than
self.response.write('html..')


w = self.response.write

w('html...')
w('more html...')
w('still more html')
Jul 19 '05 #4
On Mon, 02 May 2005 20:24:02 -0400, vegetax <ve******@gmail.com> wrote:
Irmen de Jong wrote:
vegetax wrote:
How can i use cgi'like print statement in a multitreaded web framework?
each thread has its own Servlet instance with request/response objects,
sys.stdout = self.response(which is a file like object) wont work because
all threads will set the same file object and it will be a concurrence
mess.

I am out of ideas here,so any hacks welcome =)


instead of:

print "<html>.....</html>

do:

print >>self.response, "<html>....</html>"
--Irmen


But i want to use "print" as a commodity feature, print >>
self.response,'html..' is longer to type than
self.response.write('html..')

The only clear way i am thinking right now is to write a preprocessor ,like
quixote ptl,but thats sloww

Maybe assign an object to sys.stdout that has write etc methods that check
what thread is calling and use self.response things that you assign to
a property of that sys.stdout object instead of directly to sys.stdout.

I.e., something like

sys.stdout = my_stdout_dispatcher
...
# property out_channels stores self.response with thread id:
sys.stdout.out_channels = self.response(which is a file like object)
...

# print => sys.stdout.write => my_stdout_dispatcher.write('from some thread')
# which atomically looks up right self.response
print 'from some thread'

You may need some lock stuff to do this properly
(to make self.response lookup info access effectively atomic),
but that's a general idea. I don't know what other thread interaction issues
you may have with the state of possibly mutable data being printed.

This is just an idea for an approach. I may not be understanding your problem at all ;-)

Regards,
Bengt Richter
Jul 19 '05 #5
Bengt Richter wrote:
On Mon, 02 May 2005 20:24:02 -0400, vegetax <ve******@gmail.com> wrote:
Irmen de Jong wrote:
vegetax wrote:
How can i use cgi'like print statement in a multitreaded web framework?
each thread has its own Servlet instance with request/response objects,
sys.stdout = self.response(which is a file like object) wont work
because all threads will set the same file object and it will be a
concurrence mess.

I am out of ideas here,so any hacks welcome =)
instead of:

print "<html>.....</html>

do:

print >>self.response, "<html>....</html>"
--Irmen


But i want to use "print" as a commodity feature, print >>
self.response,'html..' is longer to type than
self.response.write('html..')

The only clear way i am thinking right now is to write a preprocessor
,like quixote ptl,but thats sloww

Maybe assign an object to sys.stdout that has write etc methods that check
what thread is calling and use self.response things that you assign to
a property of that sys.stdout object instead of directly to sys.stdout.

I.e., something like

sys.stdout = my_stdout_dispatcher
...
# property out_channels stores self.response with thread id:
sys.stdout.out_channels = self.response(which is a file like object)
...

# print => sys.stdout.write => my_stdout_dispatcher.write('from some
# thread') which atomically looks up right self.response
print 'from some thread'

You may need some lock stuff to do this properly
(to make self.response lookup info access effectively atomic),
but that's a general idea. I don't know what other thread interaction
issues you may have with the state of possibly mutable data being printed.

This is just an idea for an approach. I may not be understanding your
problem at all ;-)

Regards,
Bengt Richter


Something like this:

class ThreadSpecificFile:
*********def*__init__(s):
*************self.files*=*[]
*********def*set_stdout(f,thread_id):
*************self.files[thread_id]*=*f
*********def*clean_up(thread_id):
*************del*self.files[thread_id]
*********def*write(data):
*************self.files[thread.get_ident()].write(data)

sys.stdout = ThreadSpecificFile()

Should be a lock for self.files and i am not sure how to make
thread.get_ident() atomic
Jul 19 '05 #6

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

Similar topics

8
by: Eric Lilja | last post by:
As the title, says: Why doesn't the following program print Hi Charles<newline> when run? #include <stdarg.h> #include <stdio.h> static void va_arg_example(const char *format, ...) { va_list...
37
by: Rajesh | last post by:
I was informed tht $SUBJECT was asked in M$ interview. Real imp. is no. of ways to achive it. mine sol. was.... int main() { if(printf("foo")) { ; } else { ; }
10
by: Prisoner at War | last post by:
Hi, your friendly neighborhood n00b here, just wondering why on earth the Py3K folks want to mess with a simple thing like the "print" "command" (is that what it's called, a command?), turning it...
4
by: Gilles Ganault | last post by:
Hello I'm puzzled as to why PHP (5) prints this (under FreeBSD 6.3): ========= #!/usr/local/bin/php <?php $dbh = new PDO("sqlite:test.sqlite"); $sql = "CREATE TABLE IF NOT EXISTS calls...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.