hey guys I'm trying to create a timer which counts the time spent on a page using a thread heres what I have so far:
<cfset session.time=0>
<cfthread name="timer" action="run">
<cfscript>
counter = 0;
while (counter <9000) {
sleep(1000);
session.time++;
counter ++;
}
</cfscript>
</cfthread>
page 2:
<cfoutput>#session.time#</cfoutput>
page 2 gives me 0 every time anyone see a problem?
edit:
I changed line 1 of my code to <cfset session.time=100> and now page 2 says 100, its like the stuff inside the cfscript loop isn't reassigning session.time
This works for me.
Are you sure you are using the Developer or Enterprise edition of ColdFusion and the threads have actually kicked off? I think only those editions support multi-threading.
One way to do verity your threads working is to use cfstat - you should see one request running even though your page has returned.
Another is to write output from your spawned thread - use the code snipped below to write to System.out - ideally you'll need to have CF running as a console task to do this.
<cfset session.time=0 />
<cfthread name="timer" action="run">
<cfscript>
counter = 0;
while (counter <9000) {
sleep(1000);
session.time++;
sys = createObject("java", "java.lang.System");
sys.out.println("*** [DEBUG] - #timeformat(now(),'HH:mm:ss' )# - session.time=#session.time# ");
counter ++;
}
</cfscript>
</cfthread>
Your code works fine for me. You have got an Application.cfm page setup to enable session management i.e:
<cfapplication name="#hash(getCurrentTemplatePath())#"
sessiontimeout="#createTimeSpan(0,0,20,0)#" sessionmanagement="true"/>
Code executed inside a thread has its own scope, including session. I would set a variable within the tread and then access it from within the threads scope.
i.e.
Change session.time++; to thread.time++; and then use cfthread[timer].time to get the thread's time.
Although this may not hold if you enable session management like some of the other posts discuss.
Related
This question already has an answer here:
How to emit a Qt signal daily at a given time?
(1 answer)
Closed 7 years ago.
I am using Qt5 under Windows7.
I know how to create a task using QThread, but my problem is:
How do I run it every day at 03:00AM?
I was thinking about QTimer, but it doesn't seem to be ok... it can't be linked somehow to 03:00am.
Just to make it clear: I can't use some Windows application(s). It must be coded inside my Qt app as it does some cleaning job too: cleanup history list, trim it down to 1000 lines (or whatever), etc. So, you see I can't do that using TaskScheduler or similar Windows tools...
you can use windows task scheduler to do this for you
Whats wrong with using a QTimer? I agree that a task scheduler is the better option. Here, only about 0,03% of the time code is executed it is really supposed to do something. If the exact moment is not as important you can increase the timer interval and the check-boundaries and reduce the unncessary calls. But if you prefer such a solution this should work:
someclass::someclass(){
member_timer = new QTimer(this);
QObject::connect(member_timer, SIGNAL(timeout()), this, SLOT(check_time()));
member_timer->start(30000);
member_cleanup_performed = false;
}
void someclass::check_time(){
QTimer ctime = QTime::currentTime();
if(ctime.hour() == 3 && ctime.minute() == 0){
if(member_cleanup_performed == false){
this->cleanup();
member_cleanup_performed = true;
}
}else{
member_cleanup_performed = false;
}
}
If you can use C++11, have a look at std::this_thread::sleep_until.
Run it in a separate thread and let the thread emit a signal connected to a slot in the main thread, which then performs the action. That of course requires that your application is actually running at 3 am.
I need to write a script, that takes an array of values and multithreaded way it (forks?) runs another script with a value from array as a param, but so max running forks would be set, so it would wait for script to finish if there are more than n running already. How do I do that?
There is a plugin named child_process, but not sure how to get it done, as it always waits for child termination.
Basically, in PHP it would be something like this (wrote it from head, may contain some syntax errors):
<php
declare(ticks = 1);
$data = file('data.txt');
$max=20;
$child=0;
function sig_handler($signo) {
global $child;
switch ($signo) {
case SIGCHLD:
$child -= 1;
}
}
pcntl_signal(SIGCHLD, "sig_handler");
foreach($data as $dataline){
$dataline = trim($dataline);
while($child >= $max){
sleep(1);
}
$child++;
$pid=pcntl_fork();
if($pid){
// SOMETHING WENT WRONG? NEVER HAPPENS!
}else{
exec("php processdata.php \"$dataline\"");
exit;
}//fork
}
while($child != 0){
sleep(1);
}
?>
After the conversation in the comments, here's how to have Node executing your PHP script.
Since you're calling an external command, there's no need to create a new thread. The Node.js runloop understands that calls to external commands are async operations, and it can execute all of them at the same time.
You can see different ways for executing an external process in this SO question (linked answer may be the best in your case).
However, since you're already moving everything to Node, you may even consider rewriting your "process.php" script to Node.js code. Since, as you explained, that script connects to remote servers and databases and uses nslookup (which you may not really need with Node.js), you won't need any separate thread: they're all async operations that Node.js excels at performing.
I'm writing a loadable module for Linux kernel, where I have a need to map and unmap memory pages. It happens while all interrupts are disabled. The sequence of actions looks like this:
preempt_disable();
disable_all_interrupts(&interrupt_mask_saved);
kmap_atomic(page); // here i map ONE page
do_some_work();
kunmap_atomic(page); // unmapping ONE page
restore_all_interrupts(interrupt_mask_saved);
preempt_enable();
With actions like these it all works pretty well. But when I need to map/unmap several pages (i need it to improve cpu load) like this:
preempt_disable();
disable_all_interrupts(&interrupt_mask_saved);
for (i = 0; i < page_num; i++) {
kmap_atomic(page[ i ]); // here i map several pages
}
do_some_work();
// i tried backward unmapping but the result is the same
for (i = 0; i < page_num; i++) {
kunmap_atomic(); // unmapping several pages
}
restore_all_interrupts(interrupt_mask_saved);
preempt_enable();
The system crashes. Error and info messages are shown directly in the terminal when not in graphic mode. After outputting some messages to the screen system freezes. Kernel logs are empty, but the errors which I noticed are:
scheduling while atomic
thread overran stack or stack corrupted
In Linux code I found than kunmap_atomic use preempt_schedule() and probably this is the cause of scheduling while atomic. But I rewrote my own functions of kmap_atomic and kunmap_atomic without dealing with preemptions and it still doesn't work. Actions which I do between mapping and unmapping are not the cause because I tried without them and it still freezes.
Linux kernel version: 3.0.48, Distr AltLinux 7.0.1 and Altlinux 6.0
Kernel version 3.4.62 works fine but I need exactly 3.0.48
I've been struggling with it for a while but I have no ideas. Do you have any?
You have to pair nested kmap/kunmap functions.
Like:
for (i = 0; i < page_num; i++) {
address[i] = kmap_atomic(page[i]);
}
do_some_work();
for (i = page_num - 1; i >= 0; i--) {
kunmap_atomic(address[i]);
}
calling schedule or anything that sleeps when in interrupt context is the cause
even though u have removed preempt_schedule, there might be something still sleeping and sleeping calls schedule, Check this
I need help how to unfreeze my dialog box. I'm using MFC and I have an infinite loop I want to execute when a button is pressed. However, the dialog box freezes when the infinite loop starts. Now I looked at this thread where someone was having a similar problem.
Unfortunately I tried multithreading but I found out that It can't work for me because I'm using an api that uses OLE automation and I'm getting an unhandled memory exception. I think this is because program uses the serial port and i read somewhere you can only use the handle to the serial port in one thread.
My program is simply to see if someone has dialed in to my modem and wait for them to send me a file, then hangup. Here is my loop.
while(1)
{
//get rid of input buffer
ts->_this->m_pHAScript->haReleaseRemoteInput();
ts-> _this->textBox->SetWindowTextA("thread Commence");
//wait for connected
if(success = ts->_this->m_pHAScript->haWaitForString("CONNECT",timeout))
{
//getFile
if(success = ts->_this->m_pHAScript->haWaitForXfer(5000))
{
//hangup
ts->_this->haTypeText("+++ath\r");
}
}
}
Is there a way to unfreeze the dialog box?
Add this code inside while loop:
MSG msg;
while(PeekMessage(&msg, GetSafeHwnd(), 0, 0, PM_REMOVE))
{
DispatchMessage(&msg);
}
The GUI in Windows relies on a message loop - somewhere in your code, either explicitly or hidden in a framework, there's a loop that checks for a message in a queue and processes it. If anything blocks the code from returning to that loop, the GUI gets frozen.
There are a few ways around this. One was given by David Brabant, essentially duplicating the loop. Another is to start a new "worker" thread that runs the blocking operation independently. If your message loop has a function that it calls when it is idle, i.e. no more messages are in the queue, you can do some processing there; that's not possible in your example however.
I have a problem with Silverlight application.
Suppose I have an xml file in resources stream. I get it as usual with something like this:
StreamResourceInfo sr =
Application.GetResourceStream(new Uri("uri goes there", UriKind.Relative));
var xml = XElement.Load(sr.Stream, LoadOptions.SetBaseUri);
And everything works just fine. But if the same code runs in the background thread (via async/await or, to be simple, in background worker) it always returns null.
I’ve heard about a bug in VS with similar problems (returning null) so I’ve tried to clean solution, delete obj folders etc. but nothing works — in background thread this code always return null for resources stream.
You can't access UI resources in background thread. Ideally you should access it in UI thread and pass it to background thread.