user1663148
user1663148

Reputation: 61

twisted reactor invocations on separate thread

I am working on an application that involves fetching data over tcp using twisted api. Our process is listener application that keeps listening to events and does the following..

  1. Process event notification and build dictionary to sent it to third party application
  2. In order to complete dictionary..it calls a process using twisted api to get some additional data and complete the dictionary.

I can not execute twisted api executions on main thread else after single execution..reactor stops and main thread no further progress happens..

What I want is:

  1. for each event notification--- spool a new thread to make a twisted call over tcp to get data.
  2. join mainThread with newThread to wait for its completion
  3. get results, merge with half built dictionary and send it to third party app.

Let say I listen to eventObj1 in main thread ---

processing involves steps for ex step1---step2---step3---step4--send to party.

assume step4 involves fetching data over tcp and we should wait until results are available before we can finish dictionary and send it to third party.

so as eventObj1 comes I queue it for fetching over tcp.. while doing so I say reactor.start() (reactor is started in the main thread) and after sometime I get the data and callback is invoked dict is built for event1 and send to third party.

and but I cant queue up any more events for data fetching, until I do reactor.stop() ..because until reactor.stop() is not called main thread can't go back to process eventObj2..

So I think, what I need is reactor start in separate thread..and keep queuing up events from main thread stop reactor before main program exits

Upvotes: 2

Views: 1062

Answers (1)

Glyph
Glyph

Reputation: 31860

You don't need any threads. You just want the reactor to do multiple things, which is actually the whole point of having a reactor. See this question for an explanation: Twisted reactor starting multiple times in a single program?

Upvotes: 1

Related Questions