PHP / Python / Ruby - Long running tasks with short previews
Language is irrelevant.
Just put "short" operation in function (object, if it is complex) and use it from 2 places:

web-code which needs "preview" will call it once and get its piece of data
long-running background process will call it iteratively as much as needed and will store result of function in DB instead of returning it to user immediately

Can be easily done in PHP. Long-running processeses are not a problem since 5.3
I have a web frontend which uses data from a task that must be run frequently in bulk (which takes more time than I'd like to use PHP for). The data can be stored in a database so I was planning on writing a Java application to run the task and use the database as a middleman to get the results of this task accessible to the web. 
Problem is, a "preview" of this data is needed occasionally. IE, sometimes the user needs to request a sample of the data (which can be computed quickly) to be generated on cue. 
This presents a real problem for me, this preview IS more suited for a language like PHP even though it's essentially the same task. It's just two cases, in case A there are real jobs from many users to be done so the task takes a sufficiently long time to complete (longer than a PHP request can hold), in case B it's a baby job for just one user ran on the spot which could be completed in PHP without issue. 
I don't want to write this code twice, it would make maintenance a nightmare so I kind of need to pick a language and stick with it. The frontend is designed but not implemented so I have an oppertunity to write the site in PHP, Ruby or Python if need be. I know PHP, so I would only consider switching to Python or Ruby if one of those languages offered a solution to this problem. but I know so little about these languages that I really cannot begin to know if they offer a solution without learning them (which I don't have the time for atm). 
In short, is there a non-hackish way to write short, quick event-driven code AND long-running repetitive code with Ruby on Rails or Python? If not do you have any ideas how to satifsy these two cases with either PHP or Java?